-1

我正在我的应用程序中实现“收藏夹”功能。

这个想法是“最喜欢”的页面,可以快速返回。

这是我尝试过的:

// In the Detail View Controller
- (void)favoriteRecipe
{
    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
    NSMutableArray * favoriteDrinks;

    if ([prefs objectForKey:@"favoriteDrink"] == nil)
    {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        [prefs setObject:array forKey:@"favoriteDrink"];
    }
    NSMutableArray * tempArray = [[prefs objectForKey:@"favoriteDrink"] mutableCopy];
    favoriteDrinks = tempArray;

    NSString * drinkNameString = self.drinkNameLabel.text;
    NSString * ingredientString = self.ingredientLabel.text;
    NSArray * favArray = [NSArray arrayWithObjects:drinkNameString, ingredientString, nil];
    [favoriteDrinks addObject:favArray];
    isFavorite = YES;
    [prefs setObject:favoriteDrinks forKey:@"favoriteDrink"];

    if (isFavorite == YES)
    {
        //Change the image 
    }
}

在我的 FavoriteViewController 中,我设置了一个表格。我希望显示名称,然后选择它会转到该页面。

我使用 Parse.com 作为我的后端,所以我只有一个 Detail View Controller 可以根据项目进行更改。

这是我尝试返回详细视图控制器但崩溃的地方:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    RecipeDetailViewController * recipeDetail = [[RecipeDetailViewController alloc] initWithNibName:@"RecipeDetailViewController" bundle:nil];

    PFObject * favObjects = [self.favArray objectAtIndex:indexPath.row];

    NSArray * ingredients = [favObjects objectForKey:@"ingredients"]; //Crashes Here

    recipeDetail.ingredientArray = ingredients;

    NSString * nameString = [favObjects objectForKey:@"recipe_name"];
    recipeDetail.nameString = nameString;

    [self.navigationController pushViewController:recipeDetail animated:YES];
}

我收到的错误是:

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe76bea0

任何建议将不胜感激!

4

1 回答 1

0

此错误意味着您正在尝试在数组上调用 objectForKey。看起来你从调用[self.favArray objectAtIndex:indexPath.row]中得到的回报并不是你期望得到的。你期待一个 PFObject,但似乎你得到了一个数组(如果确实是导致崩溃的行)。

查看调试器以查看实际从[self.favArray objectAtIndex:indexPath.row].

于 2014-03-24T10:46:21.670 回答