gpt4 book ai didi

swift - 将从 API for 循环检索到的数据追加到数组

转载 作者:行者123 更新时间:2023-11-28 07:42:42 31 4
gpt4 key购买 nike

我在使用 for 循环将从 API 检索到的数据追加到数组时遇到问题。我正在尝试使用数组使 TableView 中的单元格出列。我编辑了问题以符合提供的答案,但在 numberofrowsinsection(放置整数以检查更多错误)和 cellforrowat 处收到错误。

这是 View Controller 中的代码:

  class BusinessesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var businesses: [Business] = []

@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
Business.searchWithTerm(term: "Restaurants", sort: .distance, categories: nil) { (businesses, error) in
self.businesses = businesses!


}
self.tableView.reloadData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return businesses.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Business") as! SelectTableViewCell
let business = businesses[indexPath.row]
cell.BusinessName.text = business.name

return cell
}


}

这是返回的当前控制台打印输出

["sort_by": distance, "open_now": 0, "term": Restaurants, "location": 37.785771,-122.406165]
Optional([<Decided.Business: 0x600000123520>, <Decided.Business: 0x600000123700>, <Decided.Business: 0x600000123c00>, <Decided.Business: 0x600000123ca0>, <Decided.Business: 0x600000123b60>, <Decided.Business: 0x604000124060>, <Decided.Business: 0x6040001241a0>, <Decided.Business: 0x604000124240>, <Decided.Business: 0x6040001244c0>, <Decided.Business: 0x604000124560>, <Decided.Business: 0x604000124600>, <Decided.Business: 0x600000123f20>, <Decided.Business: 0x6040001246a0>, <Decided.Business: 0x604000124740>, <Decided.Business: 0x6040001247e0>, <Decided.Business: 0x604000124880>, <Decided.Business: 0x600000124240>, <Decided.Business: 0x6000001242e0>, <Decided.Business: 0x600000124380>, <Decided.Business: 0x6000001244c0>])

这是我使用的模型:

import UIKit

class Business: NSObject {
let name: String?
let address: String?
let imageURL: URL?
let categories: String?
let distance: String?
let ratingImage: UIImage?
let reviewCount: NSNumber?

init(dictionary: NSDictionary) {
name = dictionary["name"] as? String

let imageURLString = dictionary["image_url"] as? String
if imageURLString != nil {
imageURL = URL(string: imageURLString!)
} else {
imageURL = nil
}

let location = dictionary["location"] as? NSDictionary
var address = ""
if location != nil {
let addressArray = location!["display_address"] as? NSArray
if addressArray != nil {
if addressArray!.count > 0 {
address = addressArray![0] as! String
}
if addressArray!.count > 1 {
address += ", " + (addressArray![1] as! String)
}
}
}
self.address = address

let categoriesArray = dictionary["categories"] as? [NSDictionary]
if categoriesArray != nil {
var categoryNames = [String]()
for category in categoriesArray! {
let categoryName = category["title"] as! String
categoryNames.append(categoryName)
}
categories = categoryNames.joined(separator: ", ")
} else {
categories = nil
}

let distanceMeters = dictionary["distance"] as? NSNumber
if distanceMeters != nil {
let milesPerMeter = 0.000621371
distance = String(format: "%.2f mi", milesPerMeter * distanceMeters!.doubleValue)
} else {
distance = nil
}

let rating = dictionary["rating"] as? Double
if rating != nil {
switch rating {
case 1:
self.ratingImage = UIImage(named: "stars_1")
break
case 1.5:
self.ratingImage = UIImage(named: "stars_1half")
break
case 2:
self.ratingImage = UIImage(named: "stars_2")
break
case 2.5:
self.ratingImage = UIImage(named: "stars_2half")
break
case 3:
self.ratingImage = UIImage(named: "stars_3")
break
case 3.5:
self.ratingImage = UIImage(named: "stars_3half")
break
case 4:
self.ratingImage = UIImage(named: "stars_4")
break
case 4.5:
self.ratingImage = UIImage(named: "stars_4half")
break
case 5:
self.ratingImage = UIImage(named: "stars_5")
break
default:
self.ratingImage = UIImage(named: "stars_0")
break
}
} else {
self.ratingImage = UIImage(named: "stars_0")
}

reviewCount = dictionary["review_count"] as? NSNumber
}

class func businesses(array: [NSDictionary]) -> [Business] {
var businesses = [Business]()
for dictionary in array {
let business = Business(dictionary: dictionary)
businesses.append(business)
}
return businesses
}

class func searchWithTerm(term: String, completion: @escaping ([Business]?, Error?) -> Void) {
_ = YelpClient.sharedInstance.searchWithTerm(term, completion: completion)
}

class func searchWithTerm(term: String, sort: YelpSortMode?, categories: [String]?, completion: @escaping ([Business]?, Error?) -> Void) -> Void {
_ = YelpClient.sharedInstance.searchWithTerm(term, sort: sort, categories: categories, openNow: false, completion: completion)
}
}

最佳答案

您的主要错误是您将 Names 声明为 String

在 append Hot Dog Stand 之后, TableView 创建了 13 个单元格,因为字符串有 13 个字符,在 numberOfRows 中返回。

你可能想要

 Business.searchWithTerm(term: "Restaurants", sort: .distance, categories: nil) { (businesses, error) in
if let error = error { print(error); return }
self.businesses = businesses!
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Business") as! SelectTableViewCell
let business = businesses[indexPath.row]
cell.BusinessName.text = business.name

return cell
}

并删除 Names 属性

关于swift - 将从 API for 循环检索到的数据追加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51795679/

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