woutr_be woutr_be - 1 year ago 72
iOS Question

NSMutableArray addObject doesn't seem to work

I'm really new to iOS development and I'm trying to create a simple table view, so I added a Table View Controller, added my custom class to it and now I was trying to add items.

So I created a new array:

@property (nonatomic, strong) NSMutableArray *activity;

And I the viewDidLoad, I tried adding some items to the array:

for(int i = 0; i < 20; i++)
NSLog(@"Current item: %u", i);
Item *item = [[Item alloc] init];
item.time = @"14:30";
item.text = @"A simple test";
item.startedAgo = @"yesterday";
item.project = @"testProject";
[self.activity addObject:item];

NSLog(@"Activity count: %u", [self.activity count]);

The log seems to be fine, excpet when I try to output the lenght of the array, it stays 0

2012-05-02 14:12:50.772 coop_dev[15352:f803] Current item: 0


2012-05-02 14:12:50.783 coop_dev[15352:f803] Current item: 19

2012-05-02 14:12:50.784 coop_dev[15352:f803] Activity count: 0

Am I missing something here?

Answer Source

Have you initialize your activity array before adding object to it??
In viewDidLoad initialize your activity array as follows :-

self.activity = [[[NSMutableArray alloc]init]autorelease];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download