我设置了以下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];
}


第一次调用此操作时,一切正常,将对象添加到我的数组中并毫无问题地保存到NSUserDefaults,在第一次调用后,如果我尝试将其添加到favorites数组中,它将引发异常要越过休息,它会说:

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/

10-08 20:41