我正在使用NSUserDefault構建「添加到收藏夾」。將數組添加到NSMutableArray時遇到此問題。有誰知道我做錯了什麼?非常感謝你。未能將對象添加到NSMutableArray
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *favoriteRecipes = [[NSMutableArray alloc] init];
if ([prefs objectForKey:@"myFavor"] == nil) {
//create the array
NSMutableArray *array = [[NSMutableArray alloc] init];
[prefs setObject:array forKey:@"myFavor"];
[array release];
}
NSMutableArray *tempArray = [[prefs objectForKey:@"myFavor"] mutableCopy];
favoriteRecipes = tempArray;
[tempArray release];
NSArray *charArray = [[NSArray alloc] initWithObjects: @"test1", @"test2" , nil];
//add the recipe
[favoriteRecipes addObject:[charArray objectAtIndex:0]];
//save the array to NSUserDefaults
[prefs setObject:favoriteRecipes forKey:@"myFavor"];
[prefs synchronize];
我很困惑我的最愛列表,您收到運行時錯誤或無法得到期望的結果,你的預期。 – 2012-03-09 06:35:02
是的,得到運行時錯誤 – 2012-03-10 01:40:56