开发者

How to Store NSDictionary values in an array in iphone

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=bareListEventsByCategory&appid=620&category-selected=350&counties-selected=Vest-Agder,Aust-Agder"]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *object = [parser objectWithString:json_string error:nil];
    NSArray *results = [parser objectWithString:json_string error:nil];

NSDictionary *dictOne = [results objectAtIndex:0];

appDelegate.books = [[NSMutableArray alloc] init];

NSArray *activitiesArray = [dictOne objectForKey:@"events"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];
NSLog(@"%@开发者_如何学编程 - %@", [dictOne objectForKey:@"date"]);

NSLog(@"%@ - %@", [dictTwo objectForKey:@"affectedDate"]);

I want to store these two NSDictionary values in a single array


NSMutableArray *dictArray = [NSMutableArray alloc] init];

[dictArray addObject:dictOne];
[dictArray addObject:dictTwo];

If you want to insert it at specific index..

[dictArray insertObject:dictOne atIndex:0];
[dictArray insertObject:dictTwo atIndex:1];

Refer to this link.. NSMutableArray Class Reference


A shorter, but just as effective option is:

NSMutableArray *dictArray = [NSMutableArray alloc] initWithObjects: dictOne, dictTwo, nil];

This code will initialize the array with dictOne in position 0 and dictTwo in position 1. The nil at the end is there to indicate to the objective-c compiler that this argument list is finished. if you try to request the item on position 2 you will get an error.

PS: If you don't intend to add or remove items from the array, you could also use NSArray.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜