0

简单场景:我想从 Objective-C 中调用任何 Swift Package 成员函数。

我正在构建一个参考/演示/概念验证应用程序来混合 ObjC 和 Swift。

这是 Swift 方面的一个工作片段:

import Foundation
import RicPackage

final class Greetings: NSObject {
    @objc public static let shared = Greetings()
    private override init() {}
    
    @objc public func doSomething(name: String) {
        print("Do something, \(name)")
        let ricClass = RicClass()
        ricClass.sayHello()
    }
    
    @objc public func hello() {
        print("Hello Everybody!")
    }
}

这是Objective-C方面:

#import "ViewController.h"
#import "ObjcSwiftHeader-Swift.h"

@class RicClass;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Inside VC ViewDidLoad.");
    Greetings *greetings = [Greetings shared];
    [greetings hello];
    [greetings doSomethingWithName: @"Ric"];
    
    RicClass *ricClass = [RicClass alloc];
}


@end

我得到了这个编译器错误:

在此处输入图像描述

这是该项目的快照:

在此处输入图像描述

这是 Swift 包的东西:

import Foundation

public struct RicStruct {
    public private(set) var text = "Hello, World!"

    public init() {
    }
    
    public func sayHello() -> String {
        "Hello Ric!"
    }
}

public class RicClass: NSObject {
    @objc public var msg = "Mother has a feeling, I might be too appealing."
    @objc public let text = "Hello Everybody!"
    
    public override init() {}
  
    public func sayHello() {
            print(text)
        }
    
    public func doSomething() {
        print("Inside doSomething()")
    }
}

必须有一个简单的解决方案......

4

1 回答 1

1

包作为动态嵌入式框架链接到您的应用程序中,要使用它们,您应该将包的库导入源文件:

迅速:

import RicPackage

目标-C:

@import RicPackage;
于 2021-09-24T11:01:05.363 回答