Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 返回PFQuery外部带有Parse的空数组的数组_Arrays_Swift_Parse Platform_Tableview_Pfquery - Fatal编程技术网

Arrays 返回PFQuery外部带有Parse的空数组的数组

Arrays 返回PFQuery外部带有Parse的空数组的数组,arrays,swift,parse-platform,tableview,pfquery,Arrays,Swift,Parse Platform,Tableview,Pfquery,我正在尝试填充数组postCaptions。 我需要将这个数组返回给我的表视图的numberOfRowsInSection,但它每次都返回一个空数组 var postCaptions = [String]() query2.whereKey("userid", equalTo: PFUser.current()?.objectId!) query2.findObjectsInBackground(block: { (objects,

我正在尝试填充数组postCaptions。 我需要将这个数组返回给我的表视图的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属性

由于方法
findObjectsInBackground
表明它是在后台执行的,这意味着它与UI无关,因此在该函数完成之前,数组将保持为空。打印该数组时,此函数尚未完成,因此
postCaptions
将暂时为空

这也是为什么行数为零的原因

我认为您需要做的是将此查询放入
viewDidLoad
或指定的初始化器中,并像在完成块中一样调用
tableView.reloadData()
。此外,您可能需要声明一个Bool,以确定表是否正在加载,并在完成块中,在成功执行查询后将isLoading设置为false

query2.findObjectsInBackground(block: { (objects, error) in

    if let userPosted = objects {
        self.isLoading = false

        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()

     }

 })

在您的
行数部分中

if isLoading { //you need to declare isLoading 
    return 1 //maybe a dummy cell to tell user the table is loading
} else {
    return postCaptions.count
}

findObjectsInBackground
异步工作。在返回数据之前执行打印行。将打印行放入完成块。是的,但我需要延迟调用numberOfRowsInSection,直到完成块加载完毕,从而为postCaptions提供了一个从框架调用的值
numberOfRowsInSection
。填充数据源数组并调用
reloadData()
。变量不是问题所在,问题在于在调用numberOfRowsInSection之前findObjectsInBackground从未完成,因此数组始终是0I,就像这样,但是我尝试过,isLoading永远不会等于false,因为findObjectsInBackground总是在调用NumberOfRowsInSection时仍在加载。我需要等待findObjectsInBackground完成后再调用其余部分。@FinnWolff也许您应该将
reloadData()
self.isLoading=false
放在
for in
循环之外。请参见上面我编辑的答案。由于您在查询(后台线程)完成执行后调用了
reloadData()
,因此理论上,
isLoading
在设置后应为false。因为每次调用
reloadData()
时,都会调用
numberOfRows
方法。所以我在if isLoading=false返回postCaptions.count部分numberofrowsin部分添加了print(“它起作用”)。它确实有用!!!数组已填充,但当我滚动应用程序中的提要时,它崩溃了,并说:索引超出范围我认为填充数组并将其返回numberOfRowsInSection不会导致索引超出范围错误。你知道为什么会这样吗/@FinnWolff您能给我看一下
cellforrowatinexpath
方法中的代码吗?我想这就是原因。