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

How to fetch reference string from CKReference object

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

我保存CKReference如下

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

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

我目前正在按如下方式获取 CKReference 以在 table

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

let authorTitle = authorReference.recordID.recordName

cell.detailTextLabel?.text = authorTitle

在 table 查看单元格中显示 AuthorName 的记录 ID。相反,我想以字符串格式查看 AuthorName 。我如何获得它?

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

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

您也可以使用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];