0

我是编码新手,想知道是否有人可以帮助我。我是第一次使用网络服务。我在访问任何数组时都遇到了麻烦。我什么都弄不出来。

现在我可以成功地从 Web 服务获取信息并将其存储在 NSArray 中。我 NSLog 它并看到内容出现在我的控制台中。我检查了数组的计数,它是 5。

但我无法访问任何内容。错误状态:CustomTableView[4083:11303] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xff231b0

我真的很想知道如何访问数组中的各种值。在此先感谢您的帮助!:)

@property (strong, nonatomic) NSArray *searchedYummlyRecipeList;

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.searchedYummlyRecipeList = [NSJSONSerialization JSONObjectWithData:self.yummlyRecipesData options:nil error:nil];
    //test logging
    NSLog(@"Recipe List: %@ ", self.searchedYummlyRecipeList);//prints fine
    NSLog(@"Count of test array: %lu", (unsigned long)[self.searchedYummlyRecipeList count]);//prints fine 
    NSArray *testArray = [[self.searchedYummlyRecipeList objectAtIndex:1]objectForKey:@"criteria"];//breakpoint is here
    NSLog(@"test Array: %@", testArray);//doesn't get here
    NSLog(@"test Array count: %lu", (unsigned long)[testArray count]);//doesn't get here

}

nslog 输出的提取:

 Recipe List: {
attribution =     {
    html = "<a href='http://www.yummly.com/recipes/onion-soup'>onion soup recipes</a> search powered by <img src='http://static.yummly.com/api-logo.png'/>";
    logo = "http://static.yummly.com/api-logo.png";
    text = "onion soup recipes: search powered by Yummly";
    url = "http://www.yummly.com/recipes/onion-soup";
};
criteria =     {
    allowedIngredients =         (
    );
    excludedIngredients =         (
    );
    facetFields =         (
    );
    maxResults = 6;
    requirePictures = 0;
    resultsToSkip = 0;
    terms =         (
        onion,
        soup
    );
};
facetCounts =     {
};
matches =     (
            {
        attributes =             {
            course =                 (
                Soups,
                Appetizers
            );
            cuisine =                 (
                French
            );
            holiday =                 (
                Thanksgiving
            );
        };
        flavors =             {
            bitter = "0.1666666666666667";
            meaty = "0.1666666666666667";
            piquant = 0;
            salty = "0.1666666666666667";
            sour = "0.1666666666666667";
            sweet = "0.1666666666666667";
        };
        id = "French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364";
        ingredients =             (
            "beef broth",
            "minced garlic",
            "stick butter",
            "yellow onion",
            "french bread",
            "worcestershire sauce",
            "gruyere cheese",
            "low sodium chicken broth",
            "dry white wine"
        );
        rating = 0;
        recipeName = "French Onion Soup";
        smallImageUrls =             (
            "http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
            "http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
            "http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
        );
        sourceDisplayName = "The Pioneer Woman";
        totalTimeInSeconds = 0;
    },
            {
        attributes =             {
            course =                 (
                "Main Dishes"
            );
        };
        flavors =             {
            bitter = 1;
            meaty = "0.1666666666666667";
            piquant = 0;
            salty = 1;
            sour = "0.1666666666666667";
            sweet = "0.3333333333333333";
        };
        id = "Awesome-Slow-Cooker-Pot-Roast-Allrecipes";
        ingredients =             (
            "onion soup mix",
            "condensed cream of mushroom soup",
            "pot roast",
            water
        );
        rating = "4.69";
        recipeName = "Awesome Slow Cooker Pot Roast";
        smallImageUrls =             (
            "http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
            "http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
        );
        sourceDisplayName = AllRecipes;
        totalTimeInSeconds = 29400;
    },
            {
        attributes =             {
            course =                 (
                Soups
            );
        };
        flavors =             {
            bitter = "0.1666666666666667";
            meaty = "0.1666666666666667";
            piquant = "0.6666666666666666";
            salty = "0.1666666666666667";
            sour = "0.1666666666666667";
            sweet = "0.1666666666666667";
        };
        id = "Chicken-Soup-for-Knaidelach-The-Shiksa-Blog-48854";
        ingredients =             (
            "sea salt",
            "bay leave",
            onions,
            giblets,
            "fresh dill",
            "curly-leaf parsley",
            "whole cloves",
            "chopped leaves",
            "black peppercorns",
            carrot
        );
        rating = 0;
        recipeName = "Chicken Soup for Knaidelach";
        smallImageUrls =             (
            "http://i.yummly.com/Chicken-Soup-for-Knaidelach-The-Shiksa-Blog-48854-10792.s.png",
            "http://i.yummly.com/Chicken-Soup-for-Knaidelach-The-Shiksa-Blog-48854-10711.s.png",
            "http://i.yummly.com/Chicken-Soup-for-Knaidelach-The-Shiksa-Blog-48854-10617.s.png"
        );
        sourceDisplayName = "The Shiksa in the Kitchen";
        totalTimeInSeconds = 10800;
    },
            {
        attributes =             {
            course =                 (
                Soups
            );
            cuisine =                 (
                American
            );
        };
        flavors = "<null>";
        id = "Chicken-Soup-The-Pioneer-Woman-200156";
        ingredients =             (
            ribs,
            carrots,
            chicken,
            "bay leaf",
            salt,
            parsnips,
            "black pepper",
            onion,
            "chicken broth"
        );
        rating = 0;
        recipeName = "Chicken Soup";
        smallImageUrls =             (
            "http://i.yummly.com/Chicken-Soup-The-Pioneer-Woman-200156-39944.s.png"
        );
        sourceDisplayName = "The Pioneer Woman";
        totalTimeInSeconds = 0;
    },
4

2 回答 2

0

“[__NSCFDictionary objectAtIndex:]: unrecognized selector”告诉您“objectAtIndex”选择器(方法)被“发送”到(调用)一个 NSDictionary 对象。但是 objectAtIndex 不是 NSDictionary 的方法,因此会出错。

目前尚不清楚您试图访问什么“数组”,但它不是一个数组,它是一个“字典”——一个键->值映射对象。您将使用“objectForKey”来访问它。

请注意,在 Objective-C 中很容易将错误的类型放入变量中。仅仅因为变量的类型是“NSArray*”并不意味着它真的是一个NSArray——例如,您可以从JSON 解析器中得到一个字典而不是一个数组(因为这就是JSON“所说的”)。

在您上面的清单中,“searchedYummlyRecipeList”绝对是一个 NSDictionary—— {}NSLog 中的字符表明了这一点。(一个数组将被包围()。)

于 2013-02-23T14:28:30.810 回答
0

看起来你的食谱列表是一个字典,而不是一个数组。'criteria' 键的值也是一个字典(恰好包含一些数组值的字典)。

要获取标准字典,您可以执行以下操作:

NSDictionary *criteria = self.searchedYummlyRecipeList[@"criteria"];

您当然还应该修复您的 @property 声明:

@property (strong, nonatomic) NSDictionary *searchedYummlyRecipeList;

编辑

因此,为了进一步从标准字典中获取值,您可能会这样做:

NSNumber *maxResults = criteria[@"maxResults"];

或者,您可以使用键路径来获取特定值:

NSString *url = [self.searchedYummlyRecipeList valueForKeyPath:@"attribution.url"];

另一方面,匹配项是一个数组。如果要查看第一个匹配项,例如:

NSArray *matches = self.searchedYummlyRecipeList[@"matches"];
NSDictionary *firstMatch = matches[0];
NSString *firstMatchID = firstMatch[@"id"];
于 2013-02-23T14:25:08.177 回答