1

I want to add an id to a pointer field in Parse. I have 2 classes categories and Ads. In the categories class, the objectId is a String that I am using to get all the fields of the Ads class. In the Ads class, the cat field is a pointer to categories. Now I add the cat id with another column that is a String. The String field is added but the pointer column 'cat' is not added.

Here is the code:

let ex = "s8HiwfvKtQ"
let posts = PFObject(className: "Ads")
posts.setObject(ex, forKey: "cat") 

or alternatively

posts["cat"] = ex 
posts.saveInBackground()

I tried the above code and found other solutions but still cannot save the cat field.

Below is a screenshot of the categories and Ads classes: Categoris class Ads class

Error in console is:

invalid type for key cat, expected *categories, but got string (Code: 111, Version: 1.10.0)

1 Answer 1

2

It is throwing error because cat is a category object and you are setting object as String you must set an object for cat row.

you must do something like:

 let ex = PFObject(className: "categories")
        ex.objectId = "s8HiwfvKtQ"

        let posts = PFObject(className: "Ads")
        posts.setObject(ex, forKey: "cat")

        posts.saveInBackgroundWithBlock { (success, error) -> Void in
            if let error = error{
                print(error.localizedDescription)
            }else{
                print(success)
            }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

I try above code but again error in console is :"object not found for update (Code: 101, Version: 1.10.0)"
have you checked objectId. Does s8HiwfvKtQ exist in categories
yes ,now i change the objectId that is exit in categoories then error in console is:"Attempted to change an objectId to one that's already known to the OfflineStore."
object id must be an id of category not Ads i think u are using an id which exist in Ads
ya but the objectId of category column and cat of Ads column both are same.ObjectId of category is use in Ads class as pointer field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.