0

I'm trying to write a Swift query that will get the object in the Avatar table that matches the User's avatar Pointer column. The following query doesn't pull any results:

var userAvatar = self.user["avatar"]
let avatarQuery = PFQuery(className: "Avatar")
avatarQuery.whereKey("objectId", equalTo: userAvatar)
avatarQuery.limit = 1
avatarQuery.findObjectsInBackgroundWithBlock{
    (results: [PFObject]?, error: NSError?) -> Void in

    if error != nil {
        print(error)
    } else if let results = results as? [PFObject]! {
        for result in results {

I think the problem is that the whereKey clauses is looking for a String, yet userAvatar is a PFObject. I tried converting the PFObject to String but that's not possible.

Am I overthinking this? How can I just get the Avatar object that matches the PFObject stored in User -> avatar(Pointer)?

Thanks!

EDIT: Thanks to Daniel, this is the working code (I think adding the includeKey might have helped too):

let userAvatar = self.user["avatar"] as! PFObject
let avatarQuery = PFQuery(className: "Avatar")
avatarQuery.whereKey("objectId", equalTo: userAvatar.objectId!)
avatarQuery.includeKey("avatar")
avatarQuery.limit = 1
avatarQuery.findObjectsInBackgroundWithBlock{
    (results: [PFObject]?, error: NSError?) -> Void in

        if error != nil {
            print(error)
        } else if let results = results as? [PFObject]! {

            for result in results {

1 Answer 1

1

So I think your problem is that you should not be comparing a string to a PfObject the object is not a string but a price of the object may be a string so you should compare something like the object.id to a string. If that makes sense.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply! How would I go about doing this? avatarQuery.whereKey("objectId", equalTo: userAvatar.objectId) doesn't work because equalTo has to be AnyObject
you can force down cast it to an anyObject with AnyObject(userAvatar.objectId)
avatarQuery.whereKey("objectId", equalTo: userAvatar.objectId) may help
Hmm...I tried AnyObject(userAvatar.objectId) but I get 'AnyObject' cannot be constructed because it has no accessible initializers
Nevermind, that worked! I didn't need to downcast. I'll post the updated code
|

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.