解析Swift保存多个对象

时间:2016-11-21 15:43:39

标签: swift parse-platform import upload

ParseSwift 3.0

我试图一次保存一个数据,但所有代码都是将每个文件保存到同一个ObjectId。因此覆盖以前的保存。我不是很有编码经验和丢失的经验。如果有更好的方法,我愿意接受建议。如果您需要更多信息,我很乐意提供。

func importToParse() {
    var i = 1
    var data = PFObject(className:"Annotation")
    for dataFromDict in dataDict {
        data["title"] = dataFromDict["title"]
        data["place"] = dataFromDict["place"]
        data["phoneNumber"] = "+9" + (dataFromDict["phoneNumber"]?.replacingOccurrences(of: ".", with: ""))!
        data["thingsToEat"] = dataFromDict["thingsToEat"]
        data["coordinates"] = PFGeoPoint(latitude: (dataFromDict["lat"] as! NSString).doubleValue,longitude: (dataFromDict["long"] as! NSString).doubleValue)
        data.saveInBackground(block:) {
            (success: Bool, error: Error?) -> Void in
            if (success) {
                // The object has been saved.
                print("Finished #" + String(i))
                i += 1
            } else {
                // There was a problem, check error.description
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为你需要在循环中创建对象

func importToParse() {
    var i = 1
    for dataFromDict in dataDict {
        var data = PFObject(className:"Annotation")
        data["title"] = dataFromDict["title"]
        data["place"] = dataFromDict["place"]
        data["phoneNumber"] = "+9" + (dataFromDict["phoneNumber"]?.replacingOccurrences(of: ".", with: ""))!
        data["thingsToEat"] = dataFromDict["thingsToEat"]
        data["coordinates"] = PFGeoPoint(latitude: (dataFromDict["lat"] as! NSString).doubleValue,longitude: (dataFromDict["long"] as! NSString).doubleValue)
        data.saveInBackground(block:) {
            (success: Bool, error: Error?) -> Void in
            if (success) {
                // The object has been saved.
                print("Finished #" + String(i))
                i += 1
            } else {
                // There was a problem, check error.description
            }
        }
    }
}