将数据保存到Parse

时间:2016-08-18 21:30:43

标签: swift parse-platform

我有一个非常令人沮丧的问题,试图将我的数据保存到Parse。

通常情况下,这对我来说是最容易实现的部分,但这个例子对我来说是最好的。

我搜索A LOT试图找到问题所在,但没有运气。没有数据被发送到Parse(实际上是back4apps.com)

Person是为在本地保存记录而创建的类。

func saveToParse () {

    let addressQuery = PFQuery(className: "addressBook")

    print ("Saved UUID to Parse is: \(addressUUID)")

    if person!.uuid != nil
    {
        print ("Has UUID")

    addressQuery.whereKey("uuid", equalTo: person!.uuid!)
    addressQuery.getFirstObjectInBackgroundWithBlock {(objects: PFObject?, error: NSError?) -> Void in

        if error != nil {

            if let object = objects {

                object.valueForKey("firstName")?.setValue(self.nameField.text!, forKey: "firstName")
                object.valueForKey("lastName")?.setValue(self.lastNameField.text!, forKey: "lastName")
                object.valueForKey("phone")?.setValue(self.phoneField.text!, forKey: "phone")
                object.valueForKey("email")?.setValue(self.emailField.text!, forKey: "email")
                object.valueForKey("addressCity")?.setValue(self.addressCityField.text!, forKey: "addressCity")
                object.valueForKey("addressCountry")?.setValue(self.addressCountryField.text!, forKey: "addressCountry")
                object.valueForKey("entryFrom")?.setValue("Divelog Update", forKey: "entryFrom")

                let addressBookImageData = UIImageJPEGRepresentation(self.addressBookImageAdd!.image!, 0.5)
                let addressBookImageFile = PFFile(name: "addressBookImage.jpg", data: addressBookImageData!)
                object ["addressBookImage"] = addressBookImageFile

//                    object.saveEventually()

                object.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                    if (success) {
                        print ("parse save successful")
                    } else {
                        print ("parse failed")
                        print (error)
                    }
                }



        } else {
            print (error)
            }
        }
    }
    } else {

        print ("No UUID found")

        let addressObject = PFObject(className: "addressBook")

        addressObject["firstName"]          = self.nameField.text!
        addressObject["lastName"]           = self.lastNameField.text!
        addressObject["phone"]              = self.phoneField.text!
        addressObject["email"]              = self.emailField.text!
        addressObject["addressCity"]        = self.addressCityField.text!
        addressObject["addressCountry"]     = self.addressCountryField.text!
        addressObject["username"]           = PFUser.currentUser()!.username
        addressObject["uuid"]               = addressUUID
        addressObject["entryFrom"]          = "Divelog New"

        let addressBookImageData = UIImageJPEGRepresentation(self.addressBookImageAdd!.image!, 0.5)
        let addressBookImageFile = PFFile(name: "addressBookImage.jpg", data: addressBookImageData!)
        addressObject ["addressBookImage"] = addressBookImageFile

//            addressObject.saveEventually()

        addressObject.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success) {
                print ("parse save successful")
            } else {
                print ("parse failed")
                print (error)
            }
        }

    }
}

此外,此应用程序还有2个其他部分正在成功发送数据到Parse。这只是上述代码的一个问题。

1 个答案:

答案 0 :(得分:1)

这行代码从值中获取AnyObject,然后将另一个值设置为AnyObject

object.valueForKey("firstName")?.setValue(self.nameField.text!, forKey: "firstName")

您可能希望将值设置为原始对象。用以下内容替换上面的行:

object.setValue(self.nameField.text!, forKey: "firstName")