我正在尝试在 Objective-C 中使用 Swift protocol
。
首先它像这样使用 tuple
:
protocol Validation {
func validate(value:String?) -> (Bool, ValidationErrorType)
}
因为元组不适合 Objective-C,所以我决定为此使用 Dictionary
。
@objc protocol Validation {
func validate(value:String?) -> [NSNumber : ValidationErrorType]
}
结果是:
Method cannot be a member of an @objc protocol because its result type cannot be represented in Objective-C
你能分享一下你对我做错了什么的看法吗?
最佳答案
该方法具有 ValidationErrorType
作为 Dictionary
的结果,我认为这会阻止您桥接协议(protocol)。要解决这个问题,您必须使其与 objc
兼容,或者像这样将返回值直接更改为 NSDictionary
。
@objc protocol Validation {
func validate(value:String?) -> NSDictionary
}
关于ios - 删除元组后,方法不能成为@objc 协议(protocol)的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075672/