Search code examples
iosswiftobjectparse-server

Saving An Object from Another Object With Different Class Name - IOS Swift


I am trying to save an object with the info retrieved from another object by a query with the code below. I am retrieving the object from class test2 with a query and want to save the object to class test1. However with the code below, when I make itemObj=ob1, the class name of itemObj starts to be test2. How can I carry all the info from ob1 to itemObj while keeping the class name as test1? Is there anyway to do this. I searched the web but couldn't find any similar issue? Thank you.

var itemObj = PFObject(className: "test1")
                itemObj = ob1

                itemObj.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                    if (success) {

                        print("saved", terminator: "")

                    } else {

                        print("error", terminator: "")

                    }
                }

Solution

  • When you created the var itemObj, you created a pfobject with a particular object id. You should not do it. You can either save the ob1 directly or, pass it like this:

    var itemObj = PFObject(className: "test1")
    itemObj["value1"] = ob1["value1"]
    itemObj["value2"] = ob1["value2"]          
    itemObj.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                    if (success) {
    
                        print("saved", terminator: "")
    
                    } else {
    
                        print("error", terminator: "")
    
                    }
                }