简体   繁体   English

使用Swift中的块调用Objective C方法

[英]Call Objective C method with blocks from Swift

I am implementing Matrix messaging into my iOS app using their MatrixKit framework which I added to my project via Cocoapods. 我正在使用他们的MatrixKit框架将Matrix消息传递实现到我的iOS应用中,该框架是通过Cocoapods添加到项目中的。 I have everything working except for one final issue: one of the provided methods isn't recognized in my Swift file. 除了最后一个问题,我一切正常:我的Swift文件中未识别出其中一种提供的方法。 The MXSession.h file lists a method: MXSession.h文件列出了一个方法:

- (void)start:(void (^)(void))onServerSyncDone
  failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

which is used to start an MXSession. 用于启动MXSession。 I need to call this method from my Swift class, and I have tried to following to no avail: 我需要从我的Swift类中调用此方法,但我尝试遵循以下方法无济于事:

session.start({
   //success 
}, failure: {
   //failure   
})

The compiler complains that Value of type 'MXSession' has no member 'start' when it clearly does. 编译器抱怨说Value of type 'MXSession' has no member 'start'显然Value of type 'MXSession' has no member 'start' Also, strangely, I am able to access other methods in MXSession, such as: 另外,奇怪的是,我能够访问MXSession中的其他方法,例如:

- (void)resume:(void (^)(void))resumeDone;

The following: 下列:

session.resume {
    //resumed successfully
}

works perfectly. 完美地工作。 I am not too comfortable with closures in swift, am I simply making a syntax error? 我对快速关闭不太满意,我只是在犯语法错误吗? I have tried everything and can't seem to make it work. 我已经尝试了所有方法,但似乎无法使其正常工作。

Well, of course I solved it the instant I posted this question. 好吧,当然,我在发布此问题的那一刻就解决了它。 Apparently the NS_REFINED_FOR_SWIFT macro appends two underscores to the beginning of the method for use in swift. 显然, NS_REFINED_FOR_SWIFT宏在方法的开头附加了两个下划线,以便快速使用。 The following code compiles: 以下代码编译:

session.__start({
    //success
}, failure: { error in
    //failure
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM