【发布时间】:2018-08-24 12:59:33
【问题描述】:
我有一个在 Objective-C 中定义的协议,例如:
@protocol MyProtocol <NSObject>
- (void)doStuffWithDictionary:(NSDictionary*)dict
andString:(NSString*)str1
andOptionalString:(NSString*)str2
andOptionalArray:(NSArray*)arr
callback:(void (^)(id result))onSuccess;
@end
...我正在尝试在 Swift 中定义一个实现此协议的类,例如:
class MyImpl : Operation, MyProtocol {
func doStuff(withDictionary dict: [AnyHashable : Any]!,
andString str1: String!,
andOptionalString str2: String? = nil,
andOptionalArray arr: NSArray? = nil,
callback onSuccess: ((Any?) -> Void)! {
...
}
}
但是,我遇到了以下方面的构建错误:
Type 'MyImpl' does not conform to protocol 'MyProtocol'
note: candidate has non-matching type '([AnyHashable : Any]!, String!, String?, NSArray?, ((Any?) -> Void)!'
func doStuff(withDictionary dict: [AnyHashable : Any]!, andString str1: String!, andOptionalString str2: String? = nil, andOptionalArray arr: NSArray? = nil, callback onSuccess: ((Any?) -> Void)!
它似乎对andOptionalArray arr: NSArray? = nil 参数感到不安。这里使用的正确语法是什么?
【问题讨论】:
-
进入定义协议的头文件,在左上角的“Related Items”弹出框中选择“Generated Interface”。这将向您显示您必须实现的确切 Swift 方法签名。
标签: objective-c swift syntax protocols