在CloudKit Record中添加引用

时间:2016-01-09 01:41:13

标签: swift cloudkit

我正在尝试在cloudkit中创建记录和一组参考记录。 我首先创建一个类别,然后添加一些引用此类别的位置记录。 当我创建类别时,我使用timeStamp作为recordID,并尝试将RecordID设置为添加的每个位置记录的引用。 但是,当我这样做时,我确实获得了类别记录和所有创建的位置记录,但引用字段(名为Category)是空白的。 这就是我在做的事情。 我错过了什么?

 func createCategoryandLocations() {
        let container = CKContainer.defaultContainer()
        let publicDatabase = container.publicCloudDatabase
        let timestampAsString = String(format: "%f", NSDate.timeIntervalSinceReferenceDate())
        let timestampParts = timestampAsString.componentsSeparatedByString(".")
        let recordID = CKRecordID(recordName: timestampParts[0])
        let categoryRecord = CKRecord(recordType: "Category", recordID: recordID)
        categoryRecord.setObject(category.name, forKey: "CategoryName")
        let CatReference = CKReference(recordID: recordID, action: CKReferenceAction.DeleteSelf)

        publicDatabase.saveRecord(categoryRecord) {
            record, error in
            if error != nil {
                print(error?.localizedDescription)
            } else {
                print("Recorded")
                // Now upload locations for recordID
                print("refID \(CatReference)")
                for location in self.locations {
                    let coordinates = CLLocation(latitude: location.latitude, longitude: location.longitude)
                    let locationRecord = CKRecord(recordType: "Location")
                    locationRecord.setObject(location.address, forKey: "Address")
                    locationRecord.setObject(CatReference, forKey: "Categoy")
                    locationRecord.setObject(location.site,forKey: "Name")
                    locationRecord.setObject(coordinates, forKey: "Coordinates")
                    locationRecord.setObject(location.comment, forKey: "Comment")
                    publicDatabase.saveRecord(locationRecord) {
                        record, error in
                        if error != nil {
                            print(error?.localizedDescription)
                        } else {
                            print("Recorded \(location.site)")
                        }
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

因为您设置了字段"分类"而不是"类别"。它应该是:

locationRecord.setObject(CatReference, forKey: "Category")

而不是:

locationRecord.setObject(CatReference, forKey: "Categoy")