swiftyboi
swiftyboi

Reputation: 3221

How can I set a Parse objectId as a pointer in Swift?

I need to save some PFObject with two pointers. One is createdBy and it is equal to PFUser.currentUser(). The other is card and it is equal to my example variable, s8HiwfvKtQ. When I run this code, I get the following error:

[Error]: invalid type for key card, expected *Cards, but got string (Code: 111, Version: 1.7.1)

My code is here:

var ex = "s8HiwfvKtQ"

func addToWants() {
    let wants = PFObject(className:"Wants")
    wants["createdBy"] = PFUser.currentUser()
    wants["card"] = ex
    wants.save()
}

What am I doing wrong?

Upvotes: 2

Views: 1033

Answers (4)

Lucas Huang
Lucas Huang

Reputation: 4016

I think you pre-define your data schema in your Parse data browser and it detected that the type of card should be an instance of Cards instead of String. You will need to change your data schema in your data browser in order to make it through. That's what your error message says.

In Parse, it's fine for you to create data without predefining any data schemas. Once you have already pre-defined your data schema, you have to follow your rule.

Upvotes: 1

Wain
Wain

Reputation: 119031

You don't need to do a query (which is relatively expensive as you're hitting the server). Instead, create a placeholder for the object just using the objectId and use that to populate the pointer.

Upvotes: 2

swiftyboi
swiftyboi

Reputation: 3221

I figured it out. I had to do a query for the object, and use the result as the parameter for the "card" pointer.

var ex = "s8HiwfvKtQ"

func addToWants() {

    var query = PFQuery(className:"Cards")
    query.getObjectInBackgroundWithId("\(ex)") {
        (card: PFObject?, error: NSError?) -> Void in
        if error == nil && card != nil {
            let wants = PFObject(className:"Wants")
            wants["createdBy"] = PFUser.currentUser()
            wants["card"] = card
            wants.save()
        } else {
            println(error)
        }
    }
}

Upvotes: -1

Bannings
Bannings

Reputation: 10479

wants["card"] is a Cards object not String. You should get card by ex, like this:

wants["card"] = query.getObjectWithId("ParseObjectIDString")

Upvotes: 1

Related Questions