gpt4 book ai didi

arrays - 使用 Parse 在 PFQuery 之外返回空数组的数组

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

我正在尝试填充数组 postCaptions。我需要将此数组返回到 TableView 的 numberOfRowsInSection,但它每次都返回一个空数组。

  var postCaptions = [String]()

query2.whereKey("userid", equalTo: PFUser.current()?.objectId!)
query2.findObjectsInBackground(block: { (objects, error) in
if let userPosted = objects {
for object in userPosted {
if let userPost = object as? PFObject {
self.postImageFiles.append(userPost["userPostImage"] as! PFFile)
self.postLocation.append(userPost["userPostLocation"] as! String)
self.postCaptions.append(userPost["postCaption"] as! String)
print(self.postCaptions.count) //returns value of 2 for the two posts that I've made
self.tableView.reloadData()
self.refresher.endRefreshing()
}
}
}

})
print(self.postCaptions.count) //Returns empty array

我知道问题在于线程的顺序,并且我理解这一点,但我不确定我可以做什么来使数组在查询之外保持填充。我将其视为解决此问题的一种方法,但我确实还没有找到如何在代码中解决此问题的直接答案。如果有人可以提供适用于我的代码的答案,那就太棒了!:) 我已经被这个问题困扰了一个多星期了

 var postCaptions = [String]() {
didSet {
// Do any execution that needs to wait for postCaptions here.
}
}

**cellForRowAt

     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userPhotoFeedCell", for: indexPath) as! FeedCell

if PFUser.current()?.objectId != nil {

cell.userUsername.text = PFUser.current()?.username

}


//downloads images for user profile pic
imageFiles[indexPath.row].getDataInBackground { (data, error) in

if let imageData = data {

let downloadedImage = UIImage(data: imageData)

cell.userProfilePic.image = downloadedImage

}


}

//downloades images and items to populate user post
postImageFiles[indexPath.row].getDataInBackground { (data, error) in

if let userPostImageData = data {

let downloadedUserPostImage = UIImage(data: userPostImageData)

cell.userFeedPhoto.image = downloadedUserPostImage

}


}

cell.postLocation.text = postLocation[indexPath.row]

cell.postCaption.text = postCaptions[indexPath.row]

cell.userFeedPhoto.image = UIImage(named: "OrangeFuego")

cell.userProfilePic.image = UIImage(named: "usericon.png")

return cell

}

最佳答案

如果我理解你的问题,那就相当简单了。设置var postCaptions = [String]()作为所需类中的全局变量。然后按照您所做的方式将迭代中的值附加到其中。然后可以在代码的其他部分访问 postCaptions 值,并且可以使用 count 属性。

关于arrays - 使用 Parse 在 PFQuery 之外返回空数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221038/

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