简体   繁体   中英

Can't add to an NSMutableArray with addObject method

I am having an issue with the addObject method of an NSMutableArrayObject. Here's the code I'm using right now:

- (void)addBirdSightingWithName:(NSString *)name location:(NSString *)location {
    BirdSighting *bird;
    NSDate *today = [NSDate date];
    bird = [[BirdSighting alloc] initWithName:name location:location date:today];
    [self.masterBirdSightingList addObject:bird];
    NSLog(@"Elements: %d", [self.masterBirdSightingList count]);
}

When this code runs, the NSLog call prints the value 0 to the console. I don't know what could be causing this.

EDIT:

I have looked deeper into the code, and I have discovered that the problem is that my BirdSightingDataController is never initialized. Now my question is: Where can I place the init for my BirdSightingDataController ? In the viewDidLoad ?

Thanks to everyone for the help.

alloc吃内存masterBirdSightingList

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

In almost every case where cellForRowAtIndexPath: is not called is because numberOfRowsInSection: returns 0.

Place a log there and make sure you return more than one item and you should be able to see your cells. If you need further help please post the code in your:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Check your property accessor. Are you sure it's returning the correct object? Make sure the name of the property matches the instance variable or you've specified it correctly (For example: @synthesize masterBirdSightingList = _masterBirdSightingList; . If the property accessor doesn't match the iVar, it will return nil. Of course, if you're manually implementing the accessor check your code there. If you're not, you could also try manually implementing it to make sure.

To do a quick check, remove the self.masterBirdSightingList and replace it with masterBirdSightingList (assuming that's the iVar name) to access the iVar directly and see what happens.

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