如何使用“ fetchWithRecordID”;在CloudKit仪表板中自动生成的ID? [英] How to use "fetchWithRecordID" for automatically generated ID in CloudKit Dashboard?

查看:78
本文介绍了如何使用“ fetchWithRecordID”;在CloudKit仪表板中自动生成的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CloudKit和iOS开发的新手。我已将一些记录手动添加到CloudKit仪表板,现在我想通过它们的ID(自动创建)来检索它们。然后,我想在UILabel中为静态tableview单元格显示记录的值。有两个问题:1)。从仪表板使用ID时,xCode出现错误(错误显示为 expected, separator);和2)。我找不到在静态tableview单元格的UILabel中放置记录值的任何示例。 (尤其是快速)。任何帮助是极大的赞赏!

I'm new to CloudKit and iOS development. I've manually added some records to the CloudKit Dashboard, and now I want to retrieve them by their IDs, which were automatically created. And then I want to display a record's value in a UILabel for a static tableview cell. Having two issues: 1). I'm getting an error in xCode when using the ID from the dashboard (error says "expected "," separator") ; and 2). I can't find any examples of putting a record value in a UILabel in a static tableview cell. (especially for swift). Any help is greatly appreciated!

这是我的代码:

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仍然为空......关于上述代码或此代码在做什么方面的任何建议?

UPDATE: ALTERNATIVELY - I tried this code and the build succeeded, but console said it had an internal error and the UILabel was still blank......Any suggestions on what I'm doing wrong with the above code or this code?

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)

        }
    }
}


推荐答案

这是2个不同的问题


  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)
        }
   }
})

这篇关于如何使用“ fetchWithRecordID”;在CloudKit仪表板中自动生成的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆