ios - 访问 CKRecordZone 数组中的字段?

标签 ios arrays swift cloudkit

这是数组的样子:

Optional([<CKRecordZone: 0x155f0dc0; zoneID=_defaultZone:__defaultOwner__, capabilities=(none)>, <CKRecordZone: 0x155e8370; zoneID=MedicalRecord:__defaultOwner__, capabilities=(Atomic,Sync,Share)>])

如您所见,此数组中有两个元素,一个带有 zoneID=_defaultZone,另一个带有 zoneID=MedicalRecord。我怎样才能只获得 zoneID = MedicalRecord 的元素?我尝试了以下但它不起作用:

if let index = recordZone?.index(of: CKRecordZone(zoneName: "MedicalRecord")) {
     print("Index is: \(index)")
     self.sendHeartRate(id: (recordZone?[index].zoneID)!)
}

它永远不会运行这个 if let block ,因为 index 总是 nil...

提前致谢!

最佳答案

解释

if let block 中的索引始终为 nil,因为在这一行中:

if let index = recordZone?.index(of: CKRecordZone(zoneName: "MedicalRecord")) {

CKRecordZone(zoneName... 是一个全新的对象,与数组中已经存在的对象分开。它可能与您尝试检索的区域名称相同 ( “MedicalRecord”),但它们仍然是两个不同的对象。(见下文。)

引用

我创建的演示代码:

let zone = CKRecordZone(zoneName: "MedicalZone")
    let defaultZone = CKRecordZone.default()
    let zoneArray = [defaultZone, zone]
    let idx = zoneArray.index(of: zone)
    print("*** Zone index: \(idx)")
    let z = zoneArray[idx!]
    print("*** z: \(z)")

    if let i = zoneArray.index(of: zone) {
        print("*** Index is: \(i)")
        print("*** id: \(zoneArray[i].zoneID)!)")
    }

    let tempZ = CKRecordZone(zoneName: "MedicalZone")

    if let index = zoneArray.index(of: tempZ)) {
        print("*** Index is: \(index)")
        print("*** id: \(zoneArray[index].zoneID)!)")
    }

如您所见,数组中的第一个 MedicalZone 与之后创建的 MedicalZone 不同:

(lldb) po zone
<CKRecordZone: 0x6180000b06e0; zoneID=MedicalZone:__defaultOwner__, capabilities=(Atomic,Sync)>


(lldb) po tempZ
<CKRecordZone: 0x6180000b0800; zoneID=MedicalZone:__defaultOwner__, capabilities=(Atomic,Sync)>

关于ios - 访问 CKRecordZone 数组中的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40727951/

相关文章:

ios - 当用户在设置中更改外观时,应用程序不会更新并且 traitCollectionDidChange 不会触发

ios - 我们可以在 native webview 而不是 cordova webview 中加载 index.html 页面吗

ios - 是否可以在 xcode 中创建新的一行选项卡?

c# - 如何将 SqlDataReader 复制到数组中,然后循环遍历数组?

使用 for 循环或 switch 语句的 JavaScript 序列

ios - iPad 上 modalPresentationStyle - FormSheet 的高度到底是多少?

ios - 在 Swift 中创建 AudioQueueBufferRef 时遇到问题

ios - 调用renewCredentialsForAccount时如何处理 "ACAccountCredentialRenewResultRejected"消息

ios - 应用程序在 iOS 8 设备上运行良好但在 iOS 7 设备上崩溃

使用递归计算更大数组中子数组的出现次数