我设置了以下 IBAction
:
#define FAVORITES_KEY @"GraphViewController.Favorites"
- (IBAction)addToFavorites:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *favorites = [defaults objectForKey:FAVORITES_KEY];
if (!favorites) favorites = [NSMutableArray array];
[favorites addObject:self.program];
[defaults setObject:favorites forKey:FAVORITES_KEY];
[defaults synchronize];
}
第一次调用这个 Action 时一切正常,对象被添加到我的数组并保存到 NSUserDefaults
没有问题,在第一次调用之后它会在添加到favorites
数组,如果我尝试跨过 break,它会这样说:
Single stepping until exit from function objc_exception_throw,
which has no line number information.
Catchpoint 3 (exception thrown).
有人遇到过类似的问题或者可能知道发生了什么事吗?
最佳答案
NSUserDefaults
返回一个不可变数组,所以你需要转换它:
NSMutableArray *favorites = [[defaults objectForKey:FAVORITES_KEY] mutableCopy];
if (!favorites) favorites = [NSMutableArray new];
...
[favorites release];
关于ios - 将对象添加到 NSArray 时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9233619/