user1555112
user1555112

Reputation: 1977

ios swift parse: get data out of parse and use them

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!!

Upvotes: 0

Views: 2168

Answers (1)

Christian
Christian

Reputation: 22343

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.

Upvotes: 3

Related Questions