初始化CBCharacteristic时可以设置初始值吗?
问题描述:
不可否认,我对Apple Docs的措辞很难,所以我可能会错过一些东西。但是,似乎并非如此。初始化CBCharacteristic时可以设置初始值吗?
这是问题。
考虑:
let charSomeUUID = CBUUID(string: "12345678-1234-12234-1234-123456789qwe")
someCharacteristic = CBMutableCharacteristic(type: charSomeUUID, properties: [CBCharacteristicProperties.read, CBCharacteristicProperties.notify], value: nil, permissions: CBAttributePermissions.readable)
一个如何将提供值字段的初始值?
我看到如何在didReceiveRead
中做到这一点。但我想设置一个初始值。
我试过把一个字符串放入一个数据对象,并把它放在值的地方,似乎并不工作。苹果文档中的(Objective C)示例的swift 3等效似乎不起作用(不再?)。
感谢您的帮助。
答
道歉。
我实际上有另一块错误的代码,最终成为真正的问题。
对于未来的尊敬,这里的代码表明我最初认为不起作用的事实上是工作。
let stringValue : String = "something"
let dataValue = stringValue.data(using: String.Encoding.utf8)
// Device Info
someCharacteristic = CBMutableCharacteristic(type: someUUID, properties: CBCharacteristicProperties.read, value: dataValue, permissions: CBAttributePermissions.readable)
注意:通过此方法添加值时,您的特征只需要是只读的。还有一些事情要阅读。
未按规定使用下面的错误只读结果:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Characteristics with cached values must be read-only'
Sulthan,谢谢您的关注。对不起,混在一起。
你是什么意思,“它似乎没有工作”?你能链接不工作的Apple示例吗? – Sulthan