我在使用 AFNetworking 从 Yummly API 中提取图像时遇到问题。问题是因为某些值没有网址吗?有想法的人吗?
该方法的最后一行是有问题的。
- (void)configureForSearchResult:(SearchResult *)searchResult
{
self.recipeNameLabel.text = searchResult.recipeName;
self.ratingLabel.text = searchResult.rating;
[self.snapImageView setImageWithURL:[NSURL URLWithString:searchResult.image] placeholderImage: [UIImage imageNamed:@"Placeholder"]];
}
这就是我解析的方式:
- (SearchResult *)parseRecipe:(NSDictionary *)dictionary
{
SearchResult *searchResult = [[SearchResult alloc] init];
searchResult.recipeName = [dictionary objectForKey:@"recipeName"];
searchResult.rating = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"rating"]];
searchResult.image = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"smallImageUrls"]];
NSLog(@"%@", searchResult.image);
return searchResult;
}
由于上面的 NSLog,这就是我从 API 中得到的:
2013-06-09 22:06:19.268 Yummly[90606:11303](“ http://i.yummly.com/Game-day-bbq-chicken-mini-pizzas-310087-274540.s.jpg ”)
2013-06-09 22:06:19.268 Yummly[90606:11303] ()
2013-06-09 22:06:19.271 Yummly[90606:11303](“ http://i.yummly.com/Buffalo-Chicken-Pizza-Food-Network-2.s.jpg ”)
2013-06-09 22:06:19.273 Yummly[90606:11303] ( )
2013-06-09 22:06:19.275 Yummly[90606:11303] (" http://i.yummly.com/Julia-child_s-eggplant-pizzas-_tranches-d_aubergine-a-l_italienne_-309207-273660.s. .jpg ")
以下是返回的 JSON 示例:
{
"attribution": {
"html": "<a href='http:\/\/www.yummly.com\/recipes\/pizza'>pizza recipes<\/a> search powered by <img alt='Yummly' src='http:\/\/static.yummly.com\/api-logo.png'\/>",
"url": "http:\/\/www.yummly.com\/recipes\/pizza",
"text": "pizza recipes: search powered by Yummly",
"logo": "http:\/\/static.yummly.com\/api-logo.png"
},
"totalMatchCount": 8591,
"facetCounts": {
},
"matches": [
{
"attributes": {
"course": [
"Main Dishes",
"Appetizers",
"Lunch and Snacks"
],
"cuisine": [
"Mediterranean",
"Greek"
]
},
"flavors": null,
"rating": 5,
"id": "Greek-pizza-333514",
"smallImageUrls": [
"http:\/\/i.yummly.com\/Greek-pizza-333514-295434.s.jpg"
],
"sourceDisplayName": "How Sweet It Is",
"totalTimeInSeconds": null,
"ingredients": [
"red onion",
"olive oil",
"pizza doughs",
"artichoke hearts",
"roasted red pepper",
"kalamata olives",
"feta",
"garlic cloves",
"fresh dill",
"tomatoes",
"mozzarella cheese"
],
"recipeName": "Greek Pizza"
},
{
"attributes": {
},
"flavors": {
"salty": 0.66666666666667,
"sour": 0.5,
"sweet": 0.16666666666667,
"bitter": 0.5,
"meaty": 0.83333333333333,
"piquant": 0
},
"rating": 5,
"id": "Breakfast-pizza-305693",
"smallImageUrls": [
"http:\/\/i.yummly.com\/Breakfast-pizza-305693-270301.s.jpg"
],
"sourceDisplayName": "Smitten Kitchen",
"totalTimeInSeconds": null,
"ingredients": [
"shallot",
"large eggs",
"bacon",
"ground black pepper",
"parmesan",
"flat-leaf parsley",
"yeast",
"kosher salt",
"scallions",
"bread flour",
"mozzarella",
"chives"
],
"recipeName": "Breakfast Pizza"
},
{
"attributes": {
},
"flavors": null,
"rating": 4,
"id": "Mini-Deep-Dish-Pizzas-Martha-Stewart-191946",
"smallImageUrls": [
"http:\/\/i.yummly.com\/Mini-Deep-Dish-Pizzas-Martha-Stewart-191946-104372.s.png"
],
"sourceDisplayName": "Martha Stewart",
"totalTimeInSeconds": 1800,
"ingredients": [
"coarse salt",
"olive oil",
"ground pepper",
"vegetables",
"tomato",
"all-purpose flour",
"pizza doughs",
"shredded mozzarella"
],