我无法访问CloudKit中我的记录类型的createdAt系统字段。我已经尝试将'createdAt'和'creationDate'作为CKQueryOperation.desiredKeys参数的参数。无论哪种方式,当我使用CKRecord.object(forKey :)访问createdAt字段时,都会得到nil。

这是我的检索记录的代码:

    entries = [Entry]()

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
    let sortDescriptor = NSSortDescriptor(key: DatabaseNameStrings.recordFieldKeyCreationDate, ascending: true)
    query.sortDescriptors = [sortDescriptor]
    let queryOperation = CKQueryOperation(query: query)
    queryOperation.desiredKeys = [DatabaseNameStrings.recordFieldKeyCreationDate, DatabaseNameStrings.fieldNameText]
    queryOperation.recordFetchedBlock = {
        (record: CKRecord) in
        let entry = Entry(ckRecord: record)
        self.entries.append(entry)
    }
    queryOperation.queryCompletionBlock = {
        (cursor: CKQueryOperation.Cursor?, error: Error?) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
            if let cursor = cursor {
                let innerQueryOperation = CKQueryOperation(cursor: cursor)
                innerQueryOperation.desiredKeys = queryOperation.desiredKeys
                innerQueryOperation.recordFetchedBlock = queryOperation.recordFetchedBlock
                innerQueryOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
                queryOperationQueue.addOperation(queryOperation)
            }
        }
    }
    queryOperationQueue.addOperation(queryOperation)

这是保存键的字符串变量的代码:
enum DatabaseNameStrings {

    static let recordTypeEntry = "Entry"
    static let fieldNameCreatedAt = "createdAt"
    static let fieldNameText = "text"
    static let recordFieldKeyCreationDate = "creationDate"

}

这是我访问createdAt字段的代码:
        print(ckRecord.object(forKey: DatabaseNameStrings.recordFieldKeyCreationDate) as Any)
        print(ckRecord.object(forKey: DatabaseNameStrings.fieldNameCreatedAt) as Any)

最佳答案

我使用CKRecord.creationDate而不是CKRecord.object(forKey :)。

关于ios - 为什么我不能在CloudKit中访问CKRecord的createdAt字段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53671389/

10-12 13:37