I’ve got this data I get from an XML feed and parse as NSDictionaries. The data structure is like this:
item = {
address = {
text = "1 Union Street";
};
data = {
text = "Hello.";
};
event_date = {
text = "2012-02-27";
};
event_type = {
text = pubQuiz;
};
latitude = {
text = "57.144994";
};
longitude = {
text = "-2.10143170000003";
};
name = {
text = "Mobile Tester";
};
picture = {
text = "public://pictures/event_default.png";
};
time = {
text = "23.00";
};
vname = {
text = Test;
};
}
//More items
Each sub-section, ie, address, data, event_date etc are all NSDictonaries.
I would like to iterate through the whole collection and grab the “text” inside each of the sections to create an array of “items” with these properties. I’ve tried using the for..in loop structure in Objective-C but haven’t had any success so far. Has anyone got a few pointers? I’m happy to read through any good tutorials or examples on how to traverse nested NSDictionaries.
EDIT:
Alright so, after parsing the XML I get that structure up there. What I would like to do is traverse the “item” structure to extract the “text” fields of all the inner dictionaries – something like this:
foreach(item in array of dictionaries) {
myItem.address = item.address.text;
myItem.data = item.data.text;
...
}
And so on. I hope this makes it a little clearer.
So, the part where I’m stuck is where I want to set the properties of the item while traversing the NSDictionaries. This is what I have so far:
for(NSDictionary *item in self.eventsArray) {
for (NSDictionary *key in [item allKeys]) {
NSLog(@"%@", key);
id value = [item objectForKey:key];
if ([value isKindOfClass:[NSDictionary class]]) {
NSDictionary* newDict = (NSDictionary*)value;
for (NSString *subKey in [newDict allKeys]) {
NSLog(@"%@", [newDict objectForKey:subKey]);
}
}
}
}
Which outputs:
address
1 Union Street
data
Hello.
…
I’m just not sure how to select the proper attribute in the object I’m creating to set the required property, if that makes any sense?
To get the items I did the following:
And that allows me to access all elements.