将一系列枚举从Swift桥接到Objective-C [英] Bridging an array of enums from Swift to Objective-C
问题描述
从Swift 1.2开始,就可以将Swift中的枚举自动转换为Objective-C.但是,据我所知,不可能转换一个枚举数组.这是真的吗?
Since Swift 1.2 it's been possible to automatically convert enums in Swift to Objective-C. However, as far as I can tell, it is not possible to convert an array of enums. Is this true?
因此,这是可能的:
@objc public enum SomeEnumType: Int {
case OneCase
case AnotherCase
}
但这不是:
public func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
任何人都可以验证吗?您如何建议解决此问题?一种方法是具有两个方法声明,例如:
Can anyone verify this? And how would you recommend working around this? One approach would be to have two method declarations e.g:
// This will not automatically get bridged.
public func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
// This will automatically get bridged.
public func someFunc(someArrayOfEnums: Array<Int>) -> Bool {
return true
}
但这会污染Swift界面.有什么方法可以为任何Swift使用者隐藏第二个函数声明?
But this is polluting the Swift interface. Any way to hide the second function declaration for any Swift consumers?
推荐答案
似乎,即使SomeEnumType
是@objc
,我们也无法将Array<SomeEnumType>
参数公开给Obj-C.
It seems, we cannot expose Array<SomeEnumType>
parameter to Obj-C even if SomeEnumType
is @objc
.
作为一种解决方法,如何:
As a workaround, how about:
@objc(someFunc:)
func objc_someFunc(someArrayOfEnums: Array<Int>) -> Bool {
return someFunc(someArrayOfEnums.map({ SomeEnumType(rawValue: $0)! }))
}
func someFunc(someArrayOfEnums: Array<SomeEnumType>) -> Bool {
return true
}
这篇关于将一系列枚举从Swift桥接到Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!