ios - 如何在 CloudKit 仪表板中使用 "fetchWithRecordID"自动生成 ID?

标签 ios swift cloudkit

我是 CloudKit 和 iOS 开发的新手。我已经手动将一些记录添加到 CloudKit 仪表板,现在我想通过自动创建的 ID 检索它们。然后我想在静态 TableView 单元格的 UILabel 中显示记录的值。有两个问题:1)。使用仪表板中的 ID 时,我在 xCode 中遇到错误(错误显示“预期的”、“分隔符”);和 2).我找不到任何将记录值放入静态 TableView 单元格中的 UILabel 的示例。 (特别是对于快速)。任何帮助是极大的赞赏!

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    publicDB.fetchRecordWithID (f7080e7a-f8c3-4db6-b8ee-642a011a6762) { record, error in


        if error != nil {

            println("there was an error \(error)")

        } else {

            // this is my UILabel in a static tableview. The record's only attribute is //called "subCategory"

            plasticOneValue.text = record["subCategory"]
        }   
    }
}

更新:或者 - 我尝试了这段代码并且构建成功了,但是控制台说它有一个内部错误并且 UILabel 仍然是空白......关于我在上面的代码中做错了什么的任何建议或者这段代码?

override func viewDidLoad() {
    super.viewDidLoad()

    // I created a new CKRecord ID Object and provided the existing ID in dashboard and //it fixed the error about requiring a "," separator

   var plasticOneID = CKRecordID(recordName: "f7080e7a-f8c3-4db6-b8ee-642a011a6762")

    publicDB.fetchRecordWithID (plasticOneID) { record, error in

        if error != nil {

            println("there was an error \(error)")

        } else {

            self.plasticOne.text = (record.objectForKey("subCategory") as String)

        }
    }
}

最佳答案

这是两个不同的问题

  1. 查询 ID 时,您必须像在第二个示例中那样查询 CKRecordID
  2. 访问 UI 时,您必须在主队列上执行此操作。

那么代码应该是这样的:

publicDB.fetchRecordWithID(CKRecordID(recordName: "f7080e7a-f8c3-4db6-b8ee-642a011a6762"), completionHandler: {record, error in
    if error != nil {
        println("there was an error \(error)")
    } else {
        NSOperationQueue.mainQueue().addOperationWithBlock {
            self.plasticOne.text = (record.objectForKey("subCategory") as String)
        }
   }
})

关于ios - 如何在 CloudKit 仪表板中使用 "fetchWithRecordID"自动生成 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27261948/

相关文章:

ios - Xcode 5 : Trying to link text field to UIViewController

ios - [UIImageView设置文本:]: unrecognized selector sent to instance

ios - CloudKit 错误 "Bad Container"(5/1014); "Couldn' t 从服务器获取容器配置 "iCloud.com.abc.def"">

ios - 可以在数组中添加一些填充吗?

ios - 是否可以通过NSPersistentCloudKitContainer访问creationDate?

ios - CloudKit fetchRecordChangesOperation 给出了 "AppDefaultZone does not support sync semantics"

iphone - iOS - 从应用程序获取 CPU 使用率

ios - UITableView 单元的单元测试。访问单元的 Nib

ios - 设备旋转动画在 iOS 10 中不起作用

ios - 无法在循环中切换 Bool