views:

347

answers:

1

Hi there,

I'm using Cocoa Touch to build an iPhone app.

I have an NSMutableArray called stories, that on printing to the console displays something like this:

2009-07-20 12:38:30.541 testapp[4797:20b] (
    {
    link = "http://www.testing.com";
    message = "testing";
    username = "test";
},
    {
    link = "http://www.testing2.com";
    message = "testing2";
    username = "test2";
} )

My question is, how can I loop through the array and for example print the value of 'link' each time? In PHP I'm familiar with simply going array[item] - is there any similar way in Objective-C? I'd like to loop through the array to eventually throw the data into a UITableView.

Thanks in advance.

Benji

A: 

I think you are conflating 2 things here. You want to know how to iterate through a collection (in this case an array), and how to lookup a key in a dictionary.

Objective C provides a collection iteration loop "for (OBJECT in COLLECTION)" to which will loop through every object in an array or a dictionary (in the case of a dictionary it hand you back the key that maps to an object).

In the case of your array each of the elements is a dictionary, so we can use NSDictionary's -objectForKey: to find out the value of the link and print it:

for (NSDictionary *story in stories) {
  NSLog(@"%@", [story objectForKey:@"link"]);
}
Louis Gerbarg
Perfect - thanks a lot!
Benji Barash