0

I find only in the docs how the query can look like to select data. As far as I see, there is only one way to collect 1 or many results:

var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
  (objects: [AnyObject]!, error: NSError!) -> Void in
  if error == nil {
    // The find succeeded.
    NSLog("Successfully retrieved \(objects.count) scores.")
    // Do something with the found objects
    for object in objects {
        NSLog("%@", object.objectId)
    }
  } else {
    // Log details of the failure
    NSLog("Error: %@ %@", error, error.userInfo!)
  }
}

What I cant figure out (as I am a beginner!) is how to access the object data. Lets say I have the fields "name", how can I get it? What is the right syntax? Especially if I have more than 1 result?

For just one result I would do:

var name = object["name"] as String
myArray.append(name)

Even that can't be right? To use "var xx = xx" within the loop?

And what do I do when I have more than one result?

Other thought: Declaring the var name: String! before I do the query and then:

name = object["name"] as String
self.myArray.append(name)

Returns the error: Immutable vaue of type [String] only has mutating members named 'append'

What would be the correct way to "work" with the data the query returns?

Another question: as those querys are async, they finished later and the method is "done" much more earlier, this way my array with names is empty when the view is shown and I receive the data at a later stage. What is the best practice here to have all data available before the view is delivered to the device?

Thanks so much!!

1 Answer 1

3

You can use objectForKey on your object. So instead of using var name = object["name"] as String you can use:

for object in objects {
    var name = object.valueForKey("name") as String
}

But the other parts are absolutely fine. You can create an array somewhere in you code and then add the objects to it. to do that, you can loop through your objects and than add the objects to your array. Like that:

 if error == nil {
    // The find succeeded.
    NSLog("Successfully retrieved \(objects.count) scores.")
    // Do something with the found objects
    for object in objects {
        var name = object["name"] as String
        myArray.append(name)
    }
  }

Because you can reuse the var name because every loop-element will be filled into the variable name and will erase the last value. But the array will get the new value appended. For example:

  1. First loop. The value at the first index of your objects gets loaded into the object. For example with the value "John".
  2. variable name's value is now the value of the object["name"] of the current loop. So name has the value John
  3. Now you add the value to your array.
  4. The second loop starts and the second element gets loaded inside object which now has the string Michael.
  5. The Variable name's new value is now the value of object. So name's value is now Michael

and so on.

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

3 Comments

in another post here I was reading that object["name"] is another way and not wrong? Is it really the way to use var within a loop? How can I append the result data to an array?
You can add it to a NSMutableArray by using addObjectsFromArray. I don't really know if it works that way too, but I did it like I showed you, and in my app it's working.
To avoid these errors I forgot to add var myArray = [String]().

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.