且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Objective-C中提供Swift String枚举?

更新时间:2022-12-05 09:25:07

Xcode 6.3发行说明(重点添加):


Swift语言增强功能



...

现在可以使用@objc
属性将Swift枚举导出到Objective-C。 @objc枚举必须声明整数原始类型,不能为
通用或使用关联值。因为Objective-C枚举不是
命名空间,枚举情况被导入到Objective-C作为枚举名称和案例名称的
连接。



I have this enum with String values, which will be used to tell an API method that logs to a server what kind of serverity a message has. I'm using Swift 1.2, so enums can be mapped to Objective-C

@objc enum LogSeverity : String {
    case Debug = "DEBUG"
    case Info = "INFO"
    case Warn = "WARN"
    case Error = "ERROR"
}

I get the error

@objc enum raw type String is not an integer type

I haven't managed to find anywhere which says that only integers can be translated to Objective-C from Swift. Is this the case? If so, does anyone have any best-practice suggestion on how to make something like this available in Objective-C?

From the Xcode 6.3 release notes (emphasis added):

Swift Language Enhancements

...
Swift enums can now be exported to Objective-C using the @objc attribute. @objc enums must declare an integer raw type, and cannot be generic or use associated values. Because Objective-C enums are not namespaced, enum cases are imported into Objective-C as the concatenation of the enum name and case name.