虽然使用CoreBluetooth框架将设备设置为外围管理器,但我遇到了Swift如何处理枚举的困难。Swift枚举和CBCharacteristic属性
具体而言,CBMutableCharacteristic初始化需要一个enum
为properties
论点:
init!(type UUID: CBUUID!, properties: CBCharacteristicProperties, value: NSData!, permissions: CBAttributePermissions)
CBCharacteristicProperties
是Int
-backed枚举。如果使用ObjectiveC,则初始化程序将通过使用按位OR OR |
运算符接受properties
的多个值。在斯威夫特的enum
不直接允许使用按位OR,但可以在rawValues
执行它那回的案件:
let properties = CBCharacteristicProperties.Read.rawValue | CBCharacteristicProperties.Notify.rawValue
现在,properties
是Int
型的,这意味着它可以不是传递给上述初始化程序。 ('Int' is not convertible to 'CBCharactersiticProperties'
)。因此,我试图从“原始”值创建枚举的一个实例:
let cbProperties = CBCharacteristicProperties(rawValue: properties)
然而,在运行时该解析为nil
。由按位OR产生的Int
与enum
中的任何一个都不匹配。将nil
的properties
参数传递给初始值设定项会导致运行时错误。
在Swift中可以做到这一点吗?
这并不编译。 '在调用中额外的参数'字符串' – Brad 2015-04-02 20:19:20
它适用于我在游乐场 – Paulw11 2015-04-02 20:20:25
你使用的是什么版本的Xcode?我有6.2,它会抱怨,如果我省略“字符串:”。 – Paulw11 2015-04-02 20:25:10