如何在 Cloud Kit 中创建位置字段

how to create a location field in cloud kit

我正在尝试保存带有位置字段的 CKRecord,但每当我尝试保存时它都会崩溃。这是我正在使用的代码(在它说的地方崩溃:// CRASHES HERE):

func saveStateMood(stateToSave:String) {

    // Create CK record
    let newRecord:CKRecord = CKRecord(recordType: "State")
    let newLocation:CKLocationSortDescriptor = CKLocationSortDescriptor(key: "Loco", relativeLocation: self.theState)
    newRecord.setValue(stateToSave, forKey: "State")
    newRecord.setValue(newLocation, forKey: "Loco") // CRASHES HERE!!!!!!

    // Save record into public database
    if let database = self.publicDatabase {

        database.saveRecord(newRecord, completionHandler: { (record:CKRecord!, error:NSError!) -> Void in

            // Check for error
            if error != nil {

                // There was an error
                NSLog(error.localizedDescription)

            }
            else {
                // There was no error
                dispatch_async(dispatch_get_main_queue()) {

                    // Refresh table
                    //self.retrieveStateMoods("")

                }

            }
        })
    }
}

(我正在使用 CLGeocoder 查找当前位置,然后将当前位置分配给 "theState")

我正在尝试遵循 CloudKit Quick Start 添加位置字段的指南,但它都是用 Obj-C 编写的,我似乎无法弄清楚,我也无法弄清楚如何通过位置字段获取记录。

您需要在记录中存储 CLLocation 的实例。但是您正在尝试保存 CKLocationSortDescriptor 的实例。更新您的代码以使用 CLLocation.