Swift类不符合带有错误处理的Objective-C协议

乔伊

我有Objective-C协议

@protocol SomeObjCProtocol
- (BOOL) doSomethingWithError: (NSError **)error;
@end

Swift

class SwiftClass : SomeObjCProtocol
{
    func doSomething() throws {    
    }
}

编译器给我一个 error

类型'SwiftClass'不符合协议'SomeObjCProtocol'“

有什么解决方案如何摆脱这个错误?我在用着XCode 7 Beta 4

马丁·R

有两个问题:

  • Swift 2映射func doSomething() throws到Objective-C方法- (BOOL) doSomethingAndReturnError: (NSError **)error;,这与您的协议方法不同。
  • 该协议方法必须使用该@objc属性标记为“ Objective-Ccompatible”

有两种可能的解决方案:

解决方案1:将Objective-C协议方法重命名为

@protocol SomeObjCProtocol
- (BOOL) doSomethingAndReturnError: (NSError **)error;
@end

解决方案2:保持Objective-C协议方法不变,并为Swift方法明确指定Objective-C映射:

@objc(doSomethingWithError:) func doSomething() throws {
    // Do stuff
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章