为什么Apple的枚举类型与Swift的枚举类型打印方式不同?

时间:2019-10-20 18:43:49

标签: swift macos

似乎当您从macOS SDK(可能还有其他)打印()枚举类型时,它会打印出类型名称,但是当您从自己的枚举中打印时,它会打印出案例名称。

在下面的示例中,我希望看到的是“安全案例:wpaPersonalMixed”,而不是“安全案例:CWSecurity”。

有一个技巧可以使它正常工作吗?

import CoreWLAN

// Working example
enum Numbers: Int {
    case one = 1
}
print("number case: \(Numbers.one)") // "number case: one" <-- EXPECTED
print("number raw: \(Numbers.one.rawValue)") // "number raw: 1"

// Failing example
let wifiClient: CWWiFiClient = CWWiFiClient()
let interface: CWInterface = wifiClient.interface(withName: "en0")! // interface name is specific to my machine. YMMV
let security: CWSecurity = interface.security()
print("security case: \(security)") // "security case: CWSecurity" <-- PROBLEM
print("security raw: \(security.rawValue)") // "security raw: 3"

迅捷5.1 macOS SDK 10.4

1 个答案:

答案 0 :(得分:3)

因为CWSecurity在Objective-C中声明并桥接到Swift。

添加@objc属性以将枚举公开给Objective-C,并且情况消失了。

@objc enum Numbers: Int {
    case one = 1
}

Objective-C枚举比原始Swift枚举更多。