gpt4 book ai didi

ios - 如何更新模型类?

转载 作者:行者123 更新时间:2023-11-28 23:56:03 24 4
gpt4 key购买 nike

我的 View Controller

import UIKit


class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate,UIPopoverPresentationControllerDelegate{

private var actors = [Actor]()


@IBOutlet var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
self.title = "Welcome"
tableView.delegate = self
tableView.dataSource = self
downloadJson()
tableView.tableFooterView = UIView()
}


func downloadJson() {

let reponseValue = NetworkOpertions.sharedInstace.getMethod(){ (fetchValue)-> Void in

if fetchValue != nil {
print("MY fetch Value:",fetchValue)

do {

Here how can i get data from model class

                DispatchQueue.main.async {
self.tableView.reloadData()
}

} catch {
print(error)

}

}
}

}


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

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ActorCell") as? ActorCell else { return UITableViewCell() }

cell.nameLbl.text = actors[indexPath.row].name
cell.DOBLbl.text = actors[indexPath.row].dob
cell.countryCell.text = actors[indexPath.row].country



if let imageURL = URL(string: actors[indexPath.row].image) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.imgView.image = image
}
}
}

}
return cell
}


func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}

}

我的模型类

import UIKit

class Actors: Codable {
let actors: [Actor]

init(actors: [Actor]) {
self.actors = actors
}
}

class Actor: Codable {
let name: String
let description: String
let dob: String
let country: String
let height: String
let spouse: String
let children: String
let image: String

init(name: String, description: String, dob: String, country: String, height: String, spouse: String, children: String, image: String) {
self.name = name
self.description = description
self.dob = dob
self.country = country
self.height = height
self.spouse = spouse
self.children = children
self.image = image

}

}

我的网络操作

//

import UIKit

class NetworkOpertions: NSObject {
// For Singletone class
static let sharedInstace = NetworkOpertions()

private override init() {

}

private var actors = [Actor]()

func getMethod(OnCompletion:@escaping (Bool)-> Void) {


guard let url = URL(string: FETCH_API_URL)else {return}

let session = URLSession.shared.dataTask(with:url){
(data,response,error) in

if let err = error {
print("We have an Api call error:\(err)")
return
}


if let data = data {
print("This is Data:", data)
do{
let decoder = JSONDecoder()
let downloadedActors = try decoder.decode(Actors.self, from: data)
self.actors = downloadedActors.actors
OnCompletion(true)
}

catch let err{
print(err.localizedDescription)
OnCompletion(false)

}

}
}
session.resume()

}


}

我的问题是:在我的 networkOpertions 类中,我正在更新模型类,并且想使用 viewController 中的值如何?如果我只是重新加载 self.tableView.reloadData() 它什么也没显示。有没有其他方法可以使用它

最佳答案

你可以试试

let reponseValue = NetworkOpertions.sharedInstace.getMethod(){ (fetchValue)-> Void in

if fetchValue {

DispatchQueue.main.async {
self.actors = NetworkOpertions.sharedInstace.actors
self.tableView.reloadData()
}
}

关于ios - 如何更新模型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246628/

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