[英]What's the equivalent of Objective-C's NSData.length with Swift's Data?
我正在做一些 CoreBluetooth 教程並得到了這個 obj-c 代碼:
if (request.offset > myCharacteristic.value.length) {
// stuff
}
value
是NSData
類型。 由於我的代碼在 Swift 3 中,因此 value 的類型為Data
。 但是, Data
對象沒有屬性length
。 你知道 Swift 中的等價物是什么嗎?
應該使用屬性.isEmpty
雖然.count
替換了 Swift 中的.length
,但如果您只想知道.count == 0
,那么它是低效的。
使用.count
將循環遍歷整個集合,而 '.isEmpty' 將在一個之后停止。
'.isEmpty' 也返回一個更容易閱讀和理解的 Bool ......
if data.count == 0 {
對比
if data.isEmpty {
https://developer.apple.com/documentation/foundation/data/1780284-isempty
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.