0

I am trying to get an array from a Parse.com database in my Swift app. For some reason, the array returned is always nil. I checked the permissions to make sure that the data can be accessed by my app, I find it really hard to find a starting point for this bug.

Here's my code:

    let username = PFUser.currentUser()?.objectId
    print("username: \(username)")
    let bannedUsers = PFUser.currentUser()!["bannedUsers"] as? NSArray
    print("bannedUsers: \(bannedUsers)")

The log output:

    username: Optional("Nmmtb07PoR")
    bannedUsers: nil

A screenshot from the database to show the array is not nil: enter image description here

The exact value of the array:

[{"__type":"Pointer","className":"_User","objectId":"mG33sM3fcC"}]

I tried playing around with the contents of the array, but no matter what I put in it, whether it's a pointer or just a string, I always get 'nil'. I hope you can help.

3 Answers 3

2

Solved:

I added the line

PFUser.currentUser()?.fetch()

This refreshed the user and I got the proper data instead of 'nil'. Hope it helps if you have a similar issue in the future.

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

Comments

0

How about:

let bannedUsers = PFUser.currentUser()!.bannedUsers as? NSArray

1 Comment

I get "Value of type 'PFUser' has no member 'bannedUsers'" compiler error
0

Try using: PFUser.currentUser()!.objectForKey("bannedUsers")

or PFUser.currentUser()!.valueForKey("bannedUsers")

Your column could be not included in the currentUser object so you would have to query users and include the bannedUsers column. You can do it by the following:

let query = PFUser.query()
query?.includeKey("bannedUsers")
do {
 let user = try query?.getObjectWithId((PFUser.currentUser()?.objectId)!)
 let bannedUsers = user?.valueForKey("bannedUsers")
 print(bannedUsers)
} catch {
  print(error)
}

5 Comments

It returns 'nil' in both cases
Ok, this means the pointer is not included in your current user, could you try this PFUser.currentUser()!.valueForKey("bannedUsers").fetch()
fatal error: unexpectedly found nil while unwrapping an Optional value
I added some extra columns - a new array column and a string column, in both cases I also get a 'nil'. It seems to me that for some reason I can't access user values at all
I've solved the problem - your idea with fetch() was right, it's just that I had to do fetch() on the user, not the value. The changed answer might be good, but it won't do in my case - I am using the data as a part of another query but don't want to join two queries. Still, thanks for help, I wouldn't have done it without the fetch() idea.

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.