我有问题要理解,为什么 Swift 中无法使用一组导入的(复杂的)Objective-C 接口(interface)的一些成员函数。
我有一个桥接头文件:
#import "EvernoteSDK.h"
而且我不能在我的 ViewController 中使用一些成员函数
let userStore = EvernoteUserStore()
userStore.initWithSession(session)
initWithSession 不适用于 swift 代码,但为什么?
Objective-C header 显示:
@interface EvernoteUserStore : ENAPI
+ (instancetype)userStore;
- (id)initWithSession:(EvernoteSession *)session;
如果我可以查看公开的 Objective-C header ,我可能会理解该重整是如何工作的
最佳答案
在 Swift 中,初始化调用与构造函数结合在一起。换句话说,Objective-C 的
EvernoteUserStore *userStore = [[EvernoteUserStore alloc] initWithSession:session];
成为
let userStore = EvernoteUserStore(session:session);
该工具识别 Objective-C 的 initWithSomething:
名称,并将其转换为
init(something something : SomeType)
在 EvernoteUserStore
的情况下,相应的 init
方法如下所示:
init(session session: EvernoteSession!)
关于ios - 我有办法快速查看公开的 Objective-C header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24218811/