gpt4 book ai didi

ios - 数组打印 API 调用的相同结果

转载 作者:行者123 更新时间:2023-11-30 12:26:45 25 4
gpt4 key购买 nike

我正在构建一个应用程序,您可以在其中输入成分,然后根据您的输入返回一堆食谱。我正在使用 alamofire 调用 API,这些似乎很成功。我遇到的问题是我的测试调用中的 6 个结果重复 1 个配方 6 次,而不是在单独的单元格中返回所有结果。这是API调用代码:

import Alamofire

class RecipeAp: NSObject{

var concoctions = [RecipeDetails]()

func provideRecipeDetailsForName(name: String, completed:@escaping ([RecipeDetails]) -> Void) {
let urlSearchString = URL_FULL + "onion" + "soup"
Alamofire.request(urlSearchString).responseJSON(completionHandler: { response in
let details = RecipeDetails()
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {

if let matches = dict["matches"] as? [[String: Any]] {
for ingredient in matches {
if let name = ingredient["ingredients"] as? [String] {
details.ingredients = name
self.concoctions.append(details)

}
}

for recipeName in matches {
if let name = recipeName["recipeName"] as? String {
details.recipeTitle = name
print("the recipe name = \(name.debugDescription)")
self.concoctions.append(details)

}

}

}

completed(self.concoctions)

}



})

}

}

这是我的模型:

  class RecipeDetails: NSObject {
var recipeID: String?
var recipeImageURL: String?
var recipeTitle: String?
var recipeSourceURL: String?
var recipePublisher: String?
var ingredients: [String]?
}

这是我的自定义单元格设置

   import UIKit

class RecipeListCustomCell: UITableViewCell {


@IBOutlet weak var recipeTitle: UILabel!
@IBOutlet weak var recipeUrl: UILabel!

var recipe: RecipeDetails? {

didSet {

updateView()

}

}

func updateView() {

recipeTitle.text = recipe?.recipeTitle
recipeUrl.text = recipe?.recipeSourceURL


}

}

最后这是我的 viewController

 import UIKit

class MainVC: UIViewController {

@IBOutlet weak var tableView: UITableView!

var recipe = RecipeAp()
var results = [RecipeDetails]()

override func viewDidLoad() {
super.viewDidLoad()

loadRecipes()

}


func loadRecipes() {

recipe.provideRecipeDetailsForName(name: "onion" + "soup") { (response) in

self.results = response
self.tableView.reloadData()

}

}

}

extension MainVC: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return results.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier:
"RecipeListCustomCell", for: indexPath) as! RecipeListCustomCell
let recipe = results[indexPath.row]
cell.recipe = recipe

return cell


}

}

不确定如何在每个单元格中单独显示所有食谱。我还附上了一些关于我从 API 中返回的内容和模拟器中显示的屏幕截图。

simulator output

result back from Api

最佳答案

您仅为每个响应创建一个 RecipeDetails 实例。因此,您重复地将完全相同的引用添加到您的 self.concoctions 中。

您可能需要编写如下内容:

func provideRecipeDetailsForName(name: String, completed: @escaping ([RecipeDetails]) -> Void) {
let urlSearchString = URL_FULL + "onion" + "soup"
Alamofire.request(urlSearchString).responseJSON(completionHandler: { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject> {

if let matches = dict["matches"] as? [[String: Any]] {
for match in matches {
//### Create a new instance for each iteration.
let details = RecipeDetails()
if let ingredients = match["ingredients"] as? [String] {
details.ingredients = ingredients
}
if let recipeName = match["recipeName"] as? String {
details.recipeTitle = recipeName
print("the recipe name = \(recipeName.debugDescription)")
}
//### Add the instance once in the iteration
self.concoctions.append(details)
}
}

completed(self.concoctions)
}
})
}

关于ios - 数组打印 API 调用的相同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44100565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com