gpt4 book ai didi

objective-c - 无法使用 addObject 方法添加到 NSMutableArray

转载 作者:行者123 更新时间:2023-11-29 04:45:52 25 4
gpt4 key购买 nike

我在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com