Swift类不符合具有错误处理的Objective-C协议 [英] Swift class does not conform to Objective-C protocol with error handling
本文介绍了Swift类不符合具有错误处理的Objective-C协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 Objective-C
协议
@protocol SomeObjCProtocol
- (BOOL) doSomethingWithError: (NSError **)error;
@end
And Swift
class
And Swift
class
class SwiftClass : SomeObjCProtocol
{
func doSomething() throws {
}
}
编译器给我一个错误
键入'SwiftClass'不符合协议'SomeObjCProtocol'
Type 'SwiftClass' does not conform to protocol 'SomeObjCProtocol'"
有没有解决方案如何摆脱这个错误?
我正在使用 XCode 7 Beta 4
Is there any solution how to get rid of this error?
I'm using XCode 7 Beta 4
推荐答案
有两个问题:
- Swift 2映射
func doSomething()throws
到Objective-C方法
- (BOOL)doSomethingAndReturnError:(NSError **)error; code>,这是
不同于您的协议方法。 - 协议方法必须标记为Objective-C兼容与
@objc
属性。
- Swift 2 maps
func doSomething() throws
to the Objective-C method- (BOOL) doSomethingAndReturnError: (NSError **)error;
, which is different from your protocol method. - The protocol method must be marked as "Objective-C compatible" with the
@objc
attribute.
有两种可能解决方案:
解决方案1:将Objective-C协议方法重命名为
Solution 1: Rename the Objective-C protocol method to
@protocol SomeObjCProtocol
- (BOOL) doSomethingAndReturnError: (NSError **)error;
@end
解决方案2:
离开Objective-C协议方法,并且明确指定Swift方法
的Objective-C映射:
Solution 2: Leave the Objective-C protocol method as it is, and specify the Objective-C mapping for the Swift method explicitly:
@objc(doSomethingWithError:) func doSomething() throws {
// Do stuff
}
这篇关于Swift类不符合具有错误处理的Objective-C协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文