gpt4 book ai didi

ios - 模型类 Swift 中的完成处理程序

转载 作者:行者123 更新时间:2023-11-29 05:51:27 24 4
gpt4 key购买 nike

我正在将 MVC 用于我的 Firebase 应用。现在在 Model 类中,我想显示一个包含从 Firebase 检索的数据的 TableView。每个单元格都有一个关联的图像。现在的问题是我找不到让图像显示在tableview中的方法。下载图像在后端,我找不到显示它的方法。

class List {
let ref:DatabaseReference?
var name : String
var title : String
var subtitle : String
var time : String
var puretime : Any
var indexOfSelf : String
lazy var photo : UIImage = UIImage()

init(name: String, title: String, subtitle: String, time: String, puretime: Any, indexOfSelf: String, photo: UIImage) {
self.ref = nil
self.name = name
self.title = title
self.subtitle = subtitle
self.time = time
self.puretime = puretime
self.indexOfSelf = indexOfSelf
self.photo = photo
}
typealias CompletionHandler = (_ success:Bool) -> Void
init?(snapshot: DataSnapshot)
{
guard
let value = snapshot.value as? [String: AnyObject],
let url = value["0"] as? String,
let name = value["name"] as? String,
let title = value["title"] as? String,
let subtitle = value["subtitle"] as? String,
let time = value["time"] as? String,
let puretime = value["puretime"] as? Any,
let indexOfSelf = value["indexOfSelf"] as? String
else{
return nil
}

self.ref = snapshot.ref
self.name = name
self.title = title
self.subtitle = subtitle
self.time = time
self.puretime = puretime
self.indexOfSelf = indexOfSelf
var image = UIImage()

}
var image = UIImage()
func getImage(url:String, completionHandler: @escaping CompletionHandler) {
print("Doing something!")

let storageRef = Storage.storage().reference(forURL: url)
storageRef.getData(maxSize: 1 * 2084 * 2084)
{ data, error in
if let error = error {
print("error from downloading image is \(error)")

} else {
self.image = UIImage(data: data!)!
let flag = true
completionHandler(flag)
}

}

}

func toAnyObject() -> Any{
return [
"name": name,
"title": title,
"subtitle": subtitle,
"time": time,
"puretime": puretime,
"indexOfSelf": indexOfSelf,
"photo": photo
]
}
}

除了图像之外,所有其他实体都在工作。我不太确定如何在我的案例中使用完成功能。那么有人可以给我一些建议吗?非常感谢

最佳答案

创建一个服务类并添加一个方法,如下所示(我还建议将 List 创建为结构体,而不是类。)

class YourServiceClass {
func getItemList(completion: @escaping (Array< List >?,Error?)-> Void)
{
//call your get service and inside you service response
//check for all your validation here is a success then

let listElms = arrayOfItems.compactMap(List.init)
completion(listElms,nil)

}
}

现在从您的 viewController 或 viewModel 调用 getItemList

关于ios - 模型类 Swift 中的完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634368/

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