开发者

access array member

for (int i=0; i<[data count]; i++) {
NSArray *item = [data objectAtIndex: i];
//NSString *index = item->id;
NSLog(@"%@", item);
}

This has an output of:

2011-03-07 20:48:46.949 hest[1796:207] {
    id = 1;
  开发者_如何学Python  src = "http://img0.gmodules.com/ig/images/igoogle_logo_sm.png";
}

But when doing:

NSString *index = item->id;

It fails with an output of:

struct NSArray has no member named id

What is the correct way to access members?


You can tell from the formatting of the logged object that it's not actually an NSArray. It's an NSDictionary. If you're unfamiliar with the two, here's a basic idea: an array stores objects based on an integer position. A dictionary stores things based on a "key".

To pull a value out of a dictionary, you use it's "objectForKey:" method:

id value = [item objectForKey:@"id"];


You're assigning the item returned from [data objectAtIndex: i] to a local variable typed as an NSArray. You should use the actual type you expect the data array to contain.

NSArray *item = [data objectAtIndex: i];

should be something like

MyImageDataType *item = [data objectAtIndex: i];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜