如何从CKReference对象获取引用字符串

时间:2015-05-29 21:51:59

标签: ios swift cloudkit ckreference

我有两种记录类型:Books& Authors。书籍有两个属性BookName(String)& AuthorName(CKReference)Authors有一个属性AuthorTitle(String)

我保存CKReference如下

let authorReference = CKReference(record: addBookViewController.authorTitle!, action: CKReferenceAction.DeleteSelf)

bookRecord.setObject(authorReference, forKey: "AuthorName")

我目前正在提取CKReference,如下所示,以显示在表格

let authorReference = bookRecord.objectForKey("AuthorName") as! CKReference

let authorTitle = authorReference.recordID.recordName

cell.detailTextLabel?.text = authorTitle

在tableview单元格上,它显示AuthorName的记录ID。相反,我想以字符串格式查看AuthorName。我怎么得到它?

2 个答案:

答案 0 :(得分:2)

您也可以使用CKFetchRecordsOperation

CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:@"firstRecordIDName"];
CKRecordID *recordID2 = [[CKRecordID alloc] initWithRecordName:@"secondRecordIDName"];
NSArray *arrayOfRecordIDs = @[recordID,recordID2];;
CKFetchRecordsOperation *fetchRecordsOperation = [[CKFetchRecordsOperation alloc]initWithRecordIDs:arrayOfRecordIDs];
fetchRecordsOperation.perRecordCompletionBlock = ^(CKRecord *record, CKRecordID *recordID, NSError *error) {
    if (error) {

        // handle error

    }else {

        // Successfull fetch, create data model

    }

};
fetchRecordsOperation.database = publicDatabase;
[fetchRecordsOperation start];

答案 1 :(得分:1)

您的AuthorName字段是对其他记录的引用。为了从该记录中获取数据,您需要先获取它。

database.fetchRecordWithID(CKRecordID(recordName: authorReference.recordID), completionHandler: {record, error in
 // now you can use record.objectForKey("AuthorTitle")
}