【问题标题】:Method cannot be a member of an @objc protocol after i remove tuple删除元组后,方法不能成为 @objc 协议的成员
【发布时间】:2016-08-22 09:20:00
【问题描述】:

我正在尝试在 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]
}

结果是:

方法不能是 @objc 协议的成员,因为它的结果类型不能在 Objective-C 中表示

您能否分享您对我做错了什么的看法?

【问题讨论】:

  • 什么是ValidationErrorType
  • ValidationErrorType 它是枚举:int

标签: ios swift


【解决方案1】:

该方法的结果是ValidationErrorTypeDictionary,我认为这会阻止您桥接协议。要解决问题,您必须使其与objc 兼容,或者像这样直接将返回值更改为NSDictionary

@objc protocol Validation {
    func validate(value:String?) -> NSDictionary
}

【讨论】:

  • 但在这种情况下,我的 return 语句有问题,现在返回枚举类型的操作
  • 您可以为此返回枚举整数,阅读此SO answer
猜你喜欢
  • 2016-06-22
  • 1970-01-01
  • 2018-12-31
  • 2019-08-17
  • 1970-01-01
  • 2022-12-12
  • 2017-01-22
  • 1970-01-01
相关资源
最近更新 更多