2012-03-09 95 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