首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问CKRecordZone数组中的字段?

访问CKRecordZone数组中的字段?
EN

Stack Overflow用户
提问于 2016-11-21 19:33:25
回答 2查看 49关注 0票数 0

下面是数组的样子:

代码语言:javascript
复制
Optional([<CKRecordZone: 0x155f0dc0; zoneID=_defaultZone:__defaultOwner__, capabilities=(none)>, <CKRecordZone: 0x155e8370; zoneID=MedicalRecord:__defaultOwner__, capabilities=(Atomic,Sync,Share)>])

如您所见,这个数组中有两个元素,一个带有zoneID=_defaultZone,另一个带有zoneID=MedicalRecord。如何使用zoneID = MedicalRecord只获得元素?我尝试了以下几种方法,但都不起作用:

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

它从来不运行这个if let块,因为index总是没有.

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2016-11-22 01:02:09

解释

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

代码语言:javascript
复制
if let index = recordZone?.index(of: CKRecordZone(zoneName: "MedicalRecord")) {

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

参考文献

我创建的演示代码:

代码语言:javascript
复制
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不同,后者是在之后创建的:

代码语言:javascript
复制
(lldb) po zone
<CKRecordZone: 0x6180000b06e0; zoneID=MedicalZone:__defaultOwner__, capabilities=(Atomic,Sync)>


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

Stack Overflow用户

发布于 2016-11-22 16:19:57

我可以通过以下操作来完成它!

代码语言:javascript
复制
let count = recordZones?.count
for item in recordZones!{
    let zoneName = (item.value(forKey: "_zoneID") as! CKRecordZoneID).value(forKey: "_zoneName") as! String
    print("zone name is: \(zoneName)")
    if(zoneName == "MedicalRecord"){
         self.sendHeartRate(id: item.zoneID, heartRate: heartRate)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40727951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档