如何在云工具包中创建位置字段

时间:2015-07-08 02:36:18

标签: ios cloudkit

我试图用一个位置字段保存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编写,我似乎无法弄明白,我也可以' t弄清楚如何通过其位置字段获取记录。

1 个答案:

答案 0 :(得分:0)

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