2

Possible Duplicate:
Having trouble adding objects to NSMutableArray in Objective C

I have a class with an NSMutabeArray property. In one of the class's methods, I am trying to add objects from another NSArray into this NSMutableArray. (The objects added are NSDictionaries). I have tried a couple of methods for this (specified below) but none of them seems t work, as the objects aren't added into the array (I can see that when I NSLog the count property of the NSMutableArray, which still returns 0.

Why is that, and what is the proper way of adding Items into an NSMutableArray?

Way 1:

NSArray * logItemsTemp = [logParsedData objectForKey:@"log"]; //items to add, NSlooged the count property and there are 3 items
[[self logItemsArray] addObjectsFromArray:logItemsTemp]; //adding items to the NSMutableArrayProperty logItems array - unsuccessful 

Way 2:

NSArray * logItemsTemp = [logParsedData objectForKey:@"log"];  //items to add, NSlooged the count property and there are 3 items
for (int i = 0; i<[logItemsTemp count]; i++) {
      [logItemsArray addObject:[logItemsTemp objectAtIndex:i]]; //adding items to the NSMutableArrayProperty logItems array - unsuccessful
}

EDIT:

NSArray * logItemsTemp = [logParsedData objectForKey:@"log"];
    if ([self logItemsArray] == NULL) {
        self.logItemsArray = [NSMutableArray new];
        NSLog(@"%@",logItemsTemp);
    }
        [[self logItemsArray] addObjectsFromArray:logItemsTemp];
Community
  • 1
  • 1
Iddo Gino
  • 342
  • 2
  • 11
  • possible duplicate of [Having trouble adding objects to NSMutableArray](http://stackoverflow.com/q/851926) [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/q/7125326), [\[NSMutableArray addObject:\] not affecting count](http://stackoverflow.com/q/3683761), [\[NSMutableArray addObject:\] not working](http://stackoverflow.com/q/1827058) – jscs Oct 27 '12 at 18:22

1 Answers1

1

Either way works. The first way is more efficient.

The reason you're not adding anything is I think that you have forgotten to make your mutable array in the first place :)

You say that self.logItemsArray.count is 0. This might be becasuse self.logItemsArray is nil :)

// Let's see what my logitemsarray is
NSLog(@"%@", self.logItemsArray);
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • well it returns null, though I have declared it as a property in the header file and i have synthesised it... – Iddo Gino Oct 27 '12 at 16:03
  • All that just means that your object can have a mutable array, not that you have given it one :) Try something like `self.logItemsArray = [NSMutableArray new];` – deanWombourne Oct 27 '12 at 16:16
  • Well I added the [NSMutableArray new] as you can see in the edit, but it still doesnt work.... – Iddo Gino Oct 27 '12 at 16:36
  • Have you checked that `logItemsTemp` isn't `nil` or empty - `NSLog(@"%@", logItemsTemp);` ? That's the only other think I can think of. – deanWombourne Oct 27 '12 at 18:33
  • I did it and that is what I got: ( { amount = "100.00"; date = "01/01/2000"; recipient = "Sushi Shop Nice"; }, { amount = "12.00"; date = "31/12/2011"; recipient = "Fuxi Israel"; }, { amount = "48.00"; date = "21/12/1997"; recipient = "Rambam Hospital Taxi"; } ) – Iddo Gino Oct 27 '12 at 19:19