Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to save [AnyObject]! return from Parse into NSArray

I am using parse to query all entries in a class using:

let query = PFQuery(className: "TestClass")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
//completion
}

I check each entry to see what is inside using:

for object in objects{
                        println(object)
                   }

The result is the following

<TestClass: 0x16e89300, objectId: QmGNNqUskK, localId: (null)> {
    property1 = 300;
    property2 = 1;
    property3 = "test";
}

My question is how do I save each object in objects in a seperate NSDictionary that will later be stored in a NSArray?

i.e. Array of DIctionaries

1 Answer

Hey Nick,

Just wondering what the use case for this is. What I would do is to make a separate key for each returned value, for instance,

var dictionaryReturned: NSMutableDictionay = [:]
for object in objects{
                        //Not sure what the method is but something along these lines
                        dictionaryReturned.addObject(Object: object, key: valueRetuned)
}

what i normally do in my apps is add it to and array so,

var arayReturned: NSMutableArray = []
for object in objects{
               arayReturned.addObject(object)
}

hope this helps a bit, Kai Aldag