无法从完成处理程序取回数组
Posted
技术标签:
【中文标题】无法从完成处理程序取回数组【英文标题】:Can't get back the array from completion handler 【发布时间】:2020-05-12 01:12:36 【问题描述】:我正在从 FireStore 读取数据并将其存储在我的数组中。我得到的数据在闭包内,所以我不得不使用 Completion Handler 将它传递到函数之外。但是当我调用完成处理函数时,我无法获取数组的值。它的所有打印都是[Project.CellComponents]。我知道肯定有一些愚蠢的错误,我很抱歉,因为我只是一个初学者。
OwnerHomeViewController:
class OwnerHomeViewController: UIViewController
@IBOutlet weak var tableView: UITableView!
var listingDetailArray:[CellComponents] = []
var docRef:DocumentReference!
var typeOfListing:String = ""
var charges:Int = 0
var rating:Float = 0.0
var nameOfListing:String = ""
var cityName:String = ""
var details:String = ""
var currency:String = ""
var days:String = ""
override func viewDidLoad()
super.viewDidLoad()
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
getDataFromDB (array) in
print(array) ////Result in console '[Project.CellComponents]'
listingDetailArray = array
func getDataFromDB(completionHandler: @escaping(_ array: [CellComponents]) -> ())
var tempArray:[CellComponents] = []
docRef = Firestore.firestore().document("/Property/ktgEh96NXXTShCOBwj8QpEM79PB3/Hotel/2")
docRef.getDocument (docSnapshot, error) in
if error != nil
return
else
guard let docSnapshot = docSnapshot, docSnapshot.exists else print("Error found");return
let myData = docSnapshot.data()
print(myData?["Type"] as? String ?? "")
self.typeOfListing = myData?["Type"] as? String ?? "Not Found"
self.charges = Int(myData?["Charges"] as? String ?? "Not Found") ?? 0
self.nameOfListing = myData?["Title"] as? String ?? "Not Found"
self.currency = myData?["Currency"] as? String ?? "Not Found"
self.days = myData?["Days"] as? String ?? "Not Found"
self.details = myData?["Description"] as? String ?? "Not Found"
let data2 = CellComponents(image: UIImage(named: "b")!, typeOfListing: self.typeOfListing , charges: self.charges, rating: 4.1, nameOfListing: self.nameOfListing , cityName: "Rawalpindi", detail: self.details, currency: self.currency, days: self.days)
tempArray.append(data2)
completionHandler(_:tempArray)
extension OwnerHomeViewController:UITableViewDataSource,UITableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return listingDetailArray.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let listingData = listingDetailArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "OwnerHomeCell") as! OwnerHomeTableViewCell
cell.setListingData(array: listingData)
cell.details.isEditable = false
return cell
还有我的 CellComponent 类
import Foundation
import UIKit
class CellComponents
var image: UIImage
var typeOfListing:String
var charges:Int
var rating:Float
var nameOfListing: String
var cityName : String
var detail : String
var currency:String
var days:String
init (image:UIImage, typeOfListing:String, charges:Int, rating:Float, nameOfListing:String, cityName:String, detail:String, currency: String, days:String)
self.image = image
self.typeOfListing = typeOfListing
self.charges = charges
self.rating = rating
self.nameOfListing = nameOfListing
self.cityName = cityName
self.detail = detail
self.currency = currency
self.days = days
【问题讨论】:
【参考方案1】:您的代码中有一些提前返回。您可以对其进行标记和调试。
【讨论】:
请准确回答! if error != nil return and guard let else return 如果这里发生错误,它不会调用completionHandler 没有。我已经检查了那部分。那里没有错误。以上是关于无法从完成处理程序取回数组的主要内容,如果未能解决你的问题,请参考以下文章
使用 jquery ajax 从验证中取回对象 Object
如何从 NSURLSessionDataTask 完成处理程序返回 NSData