nithin
nithin

Reputation: 2457

iPhone:Adding multiple nsdictionaries into NSArray

I need to add multiple NSDictionaries into an NSArray to get a JSON message.I want to get this string.

This is my code:

for (int i=0;i<[ids count]; i++) 
    {



    [dict setObject:[NSString stringWithFormat:@"%@",personName] forKey:@"customerName"];

NSArray *array = [NSArray arrayWithObject:cartDict]; 
[dict setObject:array forKey:@"OrderDetails"];


}
    NSString *request1 = [dict JSONRepresentation];


    //convert object to data
    NSData *jsonData = [NSData dataWithBytes:[request1 UTF8String] length:[request1 length]];
    //NSData* jsonData = [NSJSONSerialization dataWithJSONObject:req options:NSJSONWritingPrettyPrinted error:nil];

    //print out the data contents
    json1 = [[NSString alloc] initWithData:jsonData
                                  encoding:NSUTF8StringEncoding];

Upvotes: 1

Views: 614

Answers (1)

Nick Bull
Nick Bull

Reputation: 4286

You are just changing the values in the same instance of the cartDict. If you want to have more than one cartDict in your array, alloc and init a new NSDictionary and then add that to the array (don't forget to release after if you aren't using ARC!).

Upvotes: 2

Related Questions