user2634633
user2634633

Reputation: 529

Parse returning empty array in Swift when array has data

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.

Upvotes: 0

Views: 533

Answers (3)

user2634633
user2634633

Reputation: 529

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.

Upvotes: 2

user4478383
user4478383

Reputation: 338

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)
}

Upvotes: 0

TekMi
TekMi

Reputation: 191

How about:

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

Upvotes: 0

Related Questions