简体   繁体   English

Swift 2:@objc协议和枚举数组

[英]Swift 2: @objc protocol and enum array

I have an enumeration defined as follow: 我有一个枚举定义如下:

@objc enum MyEnum: Int {
   case Case1, Case2
}

and the following protocol: 和以下协议:

@objc protocol MyProtocol {
   func myFunc(myData: [MyEnum])
}

now the protocol throws the following error: 现在,该协议引发以下错误:

 Method cannot be a member of a @objc protocol because the type of the parameter cannot be represented in Objective-C

I don't understand why this is happening. 我不明白为什么会这样。 Why this works: 工作原理:

func MyFunc(myData: MyEnum)

but throws error with the array? 但是会引发数组错误?

I thought of passing an array of Int and then convert it back to enum but i don't like it very much. 我想传递一个Int数组,然后将其转换回枚举,但我不太喜欢。 Is there a better solution? 有更好的解决方案吗?

I'm not sure, but it shouldn't work because an Objective-C array should contain only pointers, and an enum of Int doesn't contain pointers. 我不确定,但是它不起作用,因为Objective-C数组应该只包含指针,而Int的枚举不包含指针。

You can try something like this 您可以尝试这样的事情

@objc enum MyEnum: Int {
    case Case1, Case2

    var numberValue : NSNumber {

        get {

            return NSNumber(
                integer: self.rawValue
            )
        }
    }
}

@objc protocol MyProtocol {
    func myFunc(myData: [NSNumber])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM