我有Objective-C协议
- @protocol SomeObjCProtocol
- - (BOOL) doSomethingWithError: (NSError **)error;
- @end
和Swift一样
- class SwiftClass : SomeObjCProtocol
- {
- func doSomething() throws {
- }
- }
编译器给我一个错误
Type ‘SwiftClass’ does not conform to protocol ‘SomeObjCProtocol'”
有两个问题:
> Swift 2 maps func doSomething()抛出Objective-C方法
– (BOOL)doSomethingAndReturnError:(NSError **)错误;,这是
与您的协议方法不同.
>协议方法必须使用@objc属性标记为“Objective-C compatible”.
有两种可能的解决方案:
- @protocol SomeObjCProtocol
- - (BOOL) doSomethingAndReturnError: (NSError **)error;
- @end
解决方案2:
保持Objective-C协议方法不变,并指定Swift方法的Objective-C映射
明确:
- @objc(doSomethingWithError:) func doSomething() throws {
- // Do stuff
- }