无法将userID指定为引用

时间:2015-02-16 00:16:35

标签: swift icloud cloudkit ckreference

我正在尝试为记录分配几个不同的属性。

日期&名称工作正常。阿凡达,这是另一个记录的参考,也很好。但后来我尝试为用户分配一个引用,我得到了slamyourhead的屏幕时刻。这是我的代码:

var avatarID : CKRecordID!
var userID : CKRecordID!

//avatarID has been assigned value in another function
daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
println("this is avatarID: \(avatarID!)")

containerY.fetchUserRecordIDWithCompletionHandler{
            userRecordID, error in
            if error != nil {
                println("error")
            } else {
                println("gettin close")
                self.userID = userRecordID
                println("this is userID: \(self.userID!)")
                daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
                }
            }
        publicDatabase.saveRecord(daMainUser) {
            record, error in
            if error != nil {
                println(error.localizedDescription)
            } else {
                println(record.objectForKey("nickName") as String + " recorded")
            }
        }

当我保存daMainUser并看看苹果仪表板时,我看到了#34; avatar"引用已成功保存,但没有" ownerOfThat"。以下是我从控制台获得的信息:

this is avatarID: <CKRecordID: 0x7a83d3d0; 79CE1EDC-593D-4AE0-8433-8488F857A302:(_defaultZone:__defaultOwner__)>
gettin close
this is userID: <CKRecordID: 0x7a863910; _8a15e8e6138ba234b62f25cfd3eeb66e:(_defaultZone:__defaultOwner__)>
John Doe recorded

这让我相信userID和来自另一条记录的ID不是同一种ID。但我不知道为什么和做什么。 提前谢谢

1 个答案:

答案 0 :(得分:1)

我认为你的括号错了。它应该是:

var avatarID : CKRecordID!
var userID : CKRecordID!

//avatarID has been assigned value in another function
daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
println("this is avatarID: \(avatarID!)")

containerY.fetchUserRecordIDWithCompletionHandler{
            userRecordID, error in
            if error != nil {
                println("error")
            } else {
                println("gettin close")
                self.userID = userRecordID
                println("this is userID: \(self.userID!)")
                daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
            }
        publicDatabase.saveRecord(daMainUser) {
            record, error in
            if error != nil {
                println(error.localizedDescription)
            } else {
                println(record.objectForKey("nickName") as String + " recorded")
            }
        }
     }

现在您在记录被提取之前保存。