简单场景:我想从 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()")
}
}
必须有一个简单的解决方案......