2012-03-09 134 views
0

我正在使用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]; 
+0

我很困惑我的最愛列表,您收到運行時錯誤或無法得到期望的結果,你的預期。 – 2012-03-09 06:35:02

+0

是的,得到運行時錯誤 – 2012-03-10 01:40:56

回答

1
favoriteRecipes = tempArray; 

instated of above line use be below line it will work fine 

[favoriteRecipes addObjectsFromArray:tempArray]; 
+0

謝謝。它可以工作,但我不知道爲什麼。它應該和我所做的沒有什麼不同。 – 2012-03-10 01:41:54

+0

我又有一個類似的問題。這是我的錯誤消息:'終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFArray isEqualToString:]:無法識別的選擇器發送到實例' – 2012-03-10 01:45:01

+0

我的腳本: - (void)viewDidLoad {[super viewDidLoad]; NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSMutableArray * titleArray = [[defaults objectForKey:@「myFavor1」] mutableCopy]; self.dataArray = titleArray; self.tableView.allowsMultipleSelectionDuringEditing = YES; self.deleteButton.tintColor = [UIColor redColor]; self.navigationItem.rightBarButtonItem = self.editButton; [self resetUI]; } – 2012-03-10 01:46:35

0

做這樣的事情昨天。看到我的代碼如下。我不知道你是否想要像我一樣的東西。

NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjectsAndKeys:productID,@"ID",productTitle,@"title",productPrice,@"price",imageUrl,@"image",shipping,@"shipping_day", nil]; 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSMutableArray *cartList = [[defaults objectForKey:@"CART_ITEMS"] mutableCopy]; 

if(!cartList) 
    cartList = [[NSMutableArray alloc] init]; 

if([cartList indexOfObject:tempDictionary] == NSNotFound) 
{ 
    NSLog(@"add favorite"); 
    [cartList addObject:tempDictionary]; 
    [defaults setObject:cartList forKey:@"CART_ITEMS"]; 
    [defaults synchronize]; 
    [sender setTitle:@"Remove" forState:UIControlStateNormal]; 
} 
else 
{ 
    NSLog(@"remove favorite"); 
    [cartList removeObject:tempDictionary]; 
    [defaults setObject:cartList forKey:@"CART_ITEMS"]; 
    [defaults synchronize]; 
    [sender setTitle:@"Add to cart" forState:UIControlStateNormal]; 
} 

這是我做的,每次我想從NSUserDefault

-(void)getCartList 
{ 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSMutableArray *cartList = [[defaults objectForKey:@"CART_ITEMS"] mutableCopy]; 
productListArr = [NSMutableArray arrayWithArray:cartList]; 
NSLog(@"cart list data == %@", cartList); 
} 
+0

感謝您與我分享。我'保存'工作。但'檢索'的問題。它和你的'getCartList()'非常相似。然而,我得到了運行時錯誤:終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFArray isEqualToString:]:無法識別的選擇器發送到實例 – 2012-03-10 01:50:22

+0

在哪裏以及如何得到該異常? – janusbalatbat 2012-03-10 03:04:54

+0

我的NSMUtableArray包含NSDictionary「tempDictionary」,這是我將數據分配給單元格對象的方式。 [cell.textLabel setText:[productListArr objectAtIndex:indexPath.row] objectForKey:@「text_key」]]; – janusbalatbat 2012-03-11 04:46:27