0

I'm trying to access the "objectId" field in "User" table, while running a query on a "Photos" table.

Here is the Parse.com query (simplified) run on "Photos" table:

func checkDbForNewPhotos() {
var query = PFQuery(className:"Photos")
query.whereKey("fbId", equalTo:"34343434343434")
query.includeKey("user.objectId"); // Is this what is needed to access user info ?

query.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]!, error: NSError!) -> Void in
    if error == nil {


        for object in objects {
            println(object) // See sample object below
            println(user.objectId) // Error here: user type doesn't have member objectId
}

println(object): Here is a sample object returned from the query. I'm trying to access 9wEGRWSnTk from the "user" field which is a pointer to another table.

<Photos: 0x7c091fd0, objectId: 6D0aHreHsC, localId: (null)> {
alerts = 0;
country1 = france;
country2 = "";
imageFile = "<PFFile: 0x7b7d9130>";
user = "<PFUser: 0x7b7d95f0, objectId: 9wEGRWSnTk>";
}

How should I format that in swift to access 9wEGRWSnTk ?

4
  • object.user.objectId? Commented Dec 17, 2014 at 13:52
  • 'String?' does not have a member named 'objectId' no matter how I force unwrap with ! Commented Dec 17, 2014 at 13:56
  • try: println(object.user) Commented Dec 17, 2014 at 13:58
  • nil : how weird isn't it ? Commented Dec 17, 2014 at 14:01

3 Answers 3

3

I had the same problem. You need to cast it to a PFUser and then access the information.

let pfObject:PFObject = object as PFObject
let userObject:PFUser = pfObject["user"] as PFUser
print("User \(userObject.objectId)")
Sign up to request clarification or add additional context in comments.

Comments

1

You should use

println(object["user"].objectId)

if the column where you store your user named as "user".

Comments

1

You could also just say EqualTo and use the PFUser.currentuser() that way you load everything from that user only else try to explain why you need the objectId when querying

query.whereKey("user", equalTo: PFUser.currentUser())

Comments

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.