我在使用 NSMutableArrayObject 的 addObject
方法时遇到问题。这是我现在使用的代码:
- (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]);
}
当此代码运行时,NSLog
调用会将值 0 打印到控制台。我不知道是什么原因造成的。
编辑:
我更深入地研究了代码,发现问题是我的 BirdSightingDataController
从未初始化。现在我的问题是:我可以将 BirdSightingDataController 的 init 放在哪里?在viewDidLoad
?
感谢大家的帮助。
最佳答案
您是否为masterBirdSightingList
分配了
内存?
self.masterBirdSightingList = [[NSMutableArray alloc] init];
关于objective-c - 无法使用 addObject 方法添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9646623/