为啥以这种方式使用 Alamofire 时没有发现任何价值?
Posted
技术标签:
【中文标题】为啥以这种方式使用 Alamofire 时没有发现任何价值?【英文标题】:Why is there no value found when using Alamofire this way?为什么以这种方式使用 Alamofire 时没有发现任何价值? 【发布时间】:2019-04-10 15:03:01 【问题描述】:我正在使用 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: "预期的 Int 值,但发现为 null。",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
,
【问题讨论】:
请edit您的问题包括您收到的 JSON 响应并导致错误。 您是否保证从 API 接收到Matches
结构中的所有数据?如果没有,请将其设为可选。似乎您没有收到至少一个食谱的 totalTimeInSeconds
。
【参考方案1】:
问题来自“计时器:result.totalTimeInSeconds”,它在某一时刻返回为零,这使得 API 调用返回“ValueNotFound”结果。
timer: result.totalTimeInSeconds ?? 0
【讨论】:
以上是关于为啥以这种方式使用 Alamofire 时没有发现任何价值?的主要内容,如果未能解决你的问题,请参考以下文章
Elixir:应用程序立即退出,或者在运行 distillery 包时没有收到输入。为啥它会以这种方式工作,以及如何解决它?