我正在尝试将一个对象添加到 nsuserdefault 中,但我得到了这个崩溃“[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object”它在这一行崩溃:[currentFav addObject:incomingBabe];
我不知道它为什么会崩溃,它在我的另一个项目上工作。
这是我的代码
-(IBAction)favorite {
NSUserDefaults *standardDefault = [NSUserDefaults standardUserDefaults];
NSMutableArray *currentFav = [[NSUserDefaults standardUserDefaults]objectForKey:@"fav"];
NSLog(@"strings stored = %@",currentFav);
NSMutableArray *newFav = [NSMutableArray arrayWithObject:[NSString stringWithFormat:@"bikini%02d.jpeg",self.currentNumber]];
if (currentFav == NULL){
currentFav = [[NSMutableArray alloc]init];
}
for(NSString *incomingBabe in newFav){
BOOL hasStringAlready = NO;
for(NSString *currentFavorite in currentFav){
if([currentFavorite isEqualToString:incomingBabe]){
hasStringAlready = YES;
NSLog(@"has string already");
break;
}
}
if (!hasStringAlready) {
[currentFav addObject:incomingBabe];
hasStringAlready = YES;
}
}
[standardDefault setObject:currentFav forKey:@"fav"];
[standardDefault synchronize];
}