为什么这样使用 Alamofire 时找不到任何值?
Why is there no value found when using Alamofire this way?
我正在使用 Alamofire 调用 Yummly.com,它应该会向我发送一个包含多个食谱的数组。在执行 API 调用时一切正常。但是当我尝试将这些多重响应添加到一个值中时,返回此消息时出现问题:
valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "matches", intValue: nil), _JSONKey(stringValue: "Index 5", intValue: 5), CodingKeys(stringValue: "totalTimeInSeconds", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))
struct RecipeSearchResult: Decodable {
let name: String?
let ingredients: String?
let image: URL?
let rating: Int?
let timer: Int?
}
struct SearchRecipesRoot: Decodable {
let matches: [Matches]
}
struct Matches: Decodable {
let recipeName: String
let smallImageUrls: [URL]
let ingredients: [String]
let id: String
let totalTimeInSeconds: Int
let rating: Int
}
func searchRecipes(from userIngredients: String) {
let urlSearchParameter = "&q=\(userIngredients)&requirePictures=true"
let searchURL = URL(
string: "https://api.yummly.com/v1/api/recipes?" + urlAPIParameter + urlSearchParameter)!
Alamofire.request(searchURL, method: .get).responseJSON {
(response) in
guard response.result.isSuccess else {
print("☠️ \(String(describing: response.result.error)) ☠️")
return
}
do {
let responseJSON = try JSONDecoder().decode(SearchRecipesRoot.self, from: response.data!)
for result in responseJSON.matches {
let recipiesSearchResult = RecipeSearchResult(
name: result.recipeName,
ingredients: result.ingredients.joined(separator: "\n"),
image: result.smallImageUrls[0],
rating: result.rating,
timer: result.totalTimeInSeconds
)
print(recipiesSearchResult)
}
}
catch {
print(error)
}
}
}
这是 JSON 的响应,其自身重复的次数与找到的食谱一样多:
{
"criteria": {
"q": "pasta tomatoes cheese salmon",
"requirePictures": true,
"allowedIngredient": null,
"excludedIngredient": null
},
"matches": [
{
"imageUrlsBySize": {
"90": "https://lh3.googleusercontent.com/7lLNUgFrzS0rHdWGYKhv4qnVg2mPkafkZzSqUWYrFCOJpV4xq_KwU5HuB8KGHdn40G-s-RQQISyaCyPdJWCxpA=s90-c"
},
"sourceDisplayName": "The Washington Post",
"ingredients": [
"dried pasta",
"olive oil",
"vidalia onion",
"garlic",
"tomatoes",
"cream cheese",
"smoked salmon",
"freshly ground black pepper",
"basil leaves",
"parmesan cheese"
],
"id": "Tomato-and-Smoked-Salmon-Pasta-2161877",
"smallImageUrls": [
"https://lh3.googleusercontent.com/R1P8lKMQZz__M77Pav5ptnX2gdzxqY1wj6xzIaxHNuFFT6xe3QQ5E-nxgEROOJ2S0GUjpNruHrsNk-c0G9fO=s90"
],
"recipeName": "Tomato and Smoked Salmon Pasta",
"totalTimeInSeconds": 2100,
"attributes": {
"course": [
"Main Dishes"
]
},
"flavors": {
"piquant": 0,
"meaty": 0.16666666666666666,
"bitter": 0.3333333333333333,
"sweet": 0.16666666666666666,
"sour": 0.3333333333333333,
"salty": 0.8333333333333334
},
"rating": 3
},
问题来自 "timer: result.totalTimeInSeconds",它在某一时刻返回 nil,这使得 API 调用返回 "ValueNotFound" 结果。
timer: result.totalTimeInSeconds ?? 0
我正在使用 Alamofire 调用 Yummly.com,它应该会向我发送一个包含多个食谱的数组。在执行 API 调用时一切正常。但是当我尝试将这些多重响应添加到一个值中时,返回此消息时出现问题:
valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "matches", intValue: nil), _JSONKey(stringValue: "Index 5", intValue: 5), CodingKeys(stringValue: "totalTimeInSeconds", intValue: nil)], debugDescription: "Expected Int value but found null instead.", underlyingError: nil))
struct RecipeSearchResult: Decodable {
let name: String?
let ingredients: String?
let image: URL?
let rating: Int?
let timer: Int?
}
struct SearchRecipesRoot: Decodable {
let matches: [Matches]
}
struct Matches: Decodable {
let recipeName: String
let smallImageUrls: [URL]
let ingredients: [String]
let id: String
let totalTimeInSeconds: Int
let rating: Int
}
func searchRecipes(from userIngredients: String) {
let urlSearchParameter = "&q=\(userIngredients)&requirePictures=true"
let searchURL = URL(
string: "https://api.yummly.com/v1/api/recipes?" + urlAPIParameter + urlSearchParameter)!
Alamofire.request(searchURL, method: .get).responseJSON {
(response) in
guard response.result.isSuccess else {
print("☠️ \(String(describing: response.result.error)) ☠️")
return
}
do {
let responseJSON = try JSONDecoder().decode(SearchRecipesRoot.self, from: response.data!)
for result in responseJSON.matches {
let recipiesSearchResult = RecipeSearchResult(
name: result.recipeName,
ingredients: result.ingredients.joined(separator: "\n"),
image: result.smallImageUrls[0],
rating: result.rating,
timer: result.totalTimeInSeconds
)
print(recipiesSearchResult)
}
}
catch {
print(error)
}
}
}
这是 JSON 的响应,其自身重复的次数与找到的食谱一样多:
{
"criteria": {
"q": "pasta tomatoes cheese salmon",
"requirePictures": true,
"allowedIngredient": null,
"excludedIngredient": null
},
"matches": [
{
"imageUrlsBySize": {
"90": "https://lh3.googleusercontent.com/7lLNUgFrzS0rHdWGYKhv4qnVg2mPkafkZzSqUWYrFCOJpV4xq_KwU5HuB8KGHdn40G-s-RQQISyaCyPdJWCxpA=s90-c"
},
"sourceDisplayName": "The Washington Post",
"ingredients": [
"dried pasta",
"olive oil",
"vidalia onion",
"garlic",
"tomatoes",
"cream cheese",
"smoked salmon",
"freshly ground black pepper",
"basil leaves",
"parmesan cheese"
],
"id": "Tomato-and-Smoked-Salmon-Pasta-2161877",
"smallImageUrls": [
"https://lh3.googleusercontent.com/R1P8lKMQZz__M77Pav5ptnX2gdzxqY1wj6xzIaxHNuFFT6xe3QQ5E-nxgEROOJ2S0GUjpNruHrsNk-c0G9fO=s90"
],
"recipeName": "Tomato and Smoked Salmon Pasta",
"totalTimeInSeconds": 2100,
"attributes": {
"course": [
"Main Dishes"
]
},
"flavors": {
"piquant": 0,
"meaty": 0.16666666666666666,
"bitter": 0.3333333333333333,
"sweet": 0.16666666666666666,
"sour": 0.3333333333333333,
"salty": 0.8333333333333334
},
"rating": 3
},
问题来自 "timer: result.totalTimeInSeconds",它在某一时刻返回 nil,这使得 API 调用返回 "ValueNotFound" 结果。
timer: result.totalTimeInSeconds ?? 0