Swift 在CloudKit记录中添加引用
我正在尝试在cloudkit中创建一个记录和一组参考记录。 我首先创建一个类别,然后添加一些引用该类别的位置记录。 创建类别时,我使用recordID的时间戳,并尝试将该recordID设置为添加的每个位置记录的引用。 但是,当我这样做时,我会得到类别记录和所有创建的位置记录,但是引用字段(命名类别)是空的。 这就是我正在做的。 我错过了什么Swift 在CloudKit记录中添加引用,swift,cloudkit,Swift,Cloudkit,我正在尝试在cloudkit中创建一个记录和一组参考记录。 我首先创建一个类别,然后添加一些引用该类别的位置记录。 创建类别时,我使用recordID的时间戳,并尝试将该recordID设置为添加的每个位置记录的引用。 但是,当我这样做时,我会得到类别记录和所有创建的位置记录,但是引用字段(命名类别)是空的。 这就是我正在做的。 我错过了什么 func createCategoryandLocations() { let container = CKContainer.defa
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)")
}
}
}
}
}
}
因为您将字段设置为“类别”,而不是“类别”。应该是:
locationRecord.setObject(CatReference, forKey: "Category")
而不是:
locationRecord.setObject(CatReference, forKey: "Categoy")
谢谢,那么我该如何设置引用类别呢?我的意思是你的域名有一个输入错误。Duh。我的问题90%都像打字错误一样简单