如何在 Objective-C 中使用 Swift String 枚举? [英] How to make a Swift String enum available in Objective-C?

查看:44
本文介绍了如何在 Objective-C 中使用 Swift String 枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有 String 值的枚举,它将用于告诉 API 方法将消息记录到服务器的服务器类型.我使用的是 Swift 1.2,所以枚举可以映射到 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"
}

我收到错误

@objc 枚举原始类型字符串不是整数类型

@objc enum raw type String is not an integer type

我还没有找到任何地方说只有整数可以从 Swift 转换为 Objective-C.是这种情况吗?如果是这样,是否有人对如何在 Objective-C 中提供此类内容有任何最佳实践建议?

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?

推荐答案

来自 Xcode 6.3 发行说明(强调):

Swift 语言增强

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

...
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.

这篇关于如何在 Objective-C 中使用 Swift String 枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆