简体   繁体   中英

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?

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];

Make sure you initialise your array before using it

self.activity = [[NSMutableArray alloc] init];

If your using ARC there is no need for autorelease.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM