与Swift的数据相比,Objective-C的NSData.length相当于什么?

时间:2017-05-28 18:49:47

标签: ios swift core-bluetooth

我正在做一些CoreBluetooth教程,并得到了这个obj-c代码:

if (request.offset > myCharacteristic.value.length) {
   // stuff
}

value的类型为NSData。由于我的代码在Swift 3中,因此值为Data。但是,Data对象没有属性length。你知道Swift中的等价物吗?

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:2)

应使用属性.isEmpty

在Swift中.count替换了.length时,如果您只想做.count == 0,效率低下。

使用.count将循环遍历整个集合,而'.isEmpty'将在一个集合后停止。

'。isEmpty'还返回一个布尔值,它更易于阅读和理解...

if data.count == 0 {

vs

if data.isEmpty {

https://developer.apple.com/documentation/foundation/data/1780284-isempty

相关问题