1

I am trying to add items from an NSArray to an NSMutableArray like so:

for(int i = 0; i < [tableData count]; i++){
        [purchaseOrder.objects addObject:[tableData objectAtIndex:i]];
        NSLog(@"%@",[tableData objectAtIndex:i]);
    }

    NSLog(@"%@", purchaseOrder.objects);

but purchaseOrder.objects returns nil: here is what NSLog(@"%@",[tableData objectAtIndex:i]); is returning:

2015-04-30 08:57:36.510 MyApp[7055:1475885] {
    POIndex = "094 Spray Foam";
    PONumber = "ABC123/028";
}
2015-04-30 08:57:36.511 MyApp[7055:1475885] {
    POIndex = "095 Drywall";
    PONumber = "ABC124/029";
}
2015-04-30 08:57:36.511 MyApp[7055:1475885] {
    POIndex = "100 Stairs";
    PONumber = "ABC125/032";
}

I am looking for away to have the PONumber as the key and POIndex as the value. Here is how my arrays are defined:

MasterController:

@property NSMutableArray *objects;

Detailer Controller:

 NSArray *tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:@"%@%@",areaPickerSelectionString,unitPickerSelectionString]];
user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

3

You must initialize your MutableArray before use .

write below code before you for loop.

if(!purchaseOrder.objects){// If Array not initialize 
    purchaseOrder.objects = [[NSMutableArray alloc]init];
}

Hope This helps you.

Malav Soni
  • 2,739
  • 1
  • 23
  • 52
0

First you need to allocate the PurchaseObjects as below

purchaseOrder.objects = [[NSMutableArray alloc]init];

And then use below code

NSMutableDictionary *subDict=[[NSMutableDictionary alloc]init];
for (NSDictionary *dict in tableData) {
    [subDict setValue:[dict objectForKey:@"POIndex"] forKey:[dict objectForKey:@"PONumber"]];
    [purchaseOrder.objects addObject:subDict];
}
NSLog(@"@@@@@@@purchaseOrder.objects Array is:%@",purchaseOrder.objects);

It will result in the array with dictionaries with required content..

Hope it helps you..

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59