ios - 尝试将数据从Parse填充到CollectionView中时出现查询错误

标签 ios swift parse-platform error-handling uicollectionview

因此,我正在制作一个应用程序,其中主 View 将显示两组集合 View 。我正在尝试根据发布的图像中的参数过滤信息的发送方式以及如何将其分配到这两个collectionview中。我的应用因此错误而崩溃。

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

下面,我放所有代码。
import UIKit
import Parse

class HomeViewController: UIViewController{

    //VAR ARRAYS - LOST
    var userslost = [String: String]()
    var addresslost = [String]()
    var breedlost = [String]()
     var phonelost = [String]()
    var usernameslost = [String]()
    var imageFileslost = [PFFile]()

    //VAR ARRAYS - FOUND
    var usersfound = [String: String]()
    var addressfound = [String]()
    var breedfound = [String]()
    var phonefound = [String]()
    var usernamesfound = [String]()
    var imageFilesfound = [PFFile]()

    //@IBOUTLETS
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var lostView: UIView!
    @IBOutlet weak var foundView: UIView!
    @IBOutlet weak var lostCollectionView: UICollectionView!
    @IBOutlet weak var foundCollectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()

        //QUERY LOST

        let querylost = PFQuery(className: "Post")
        querylost.whereKey("lostfound", equalTo: "lost")
        querylost.findObjectsInBackground { (objects, error) in

            if let posts = objects {

                for post in posts {

                    self.addresslost.append(post["address"] as! String)
                    self.breedlost.append(post["breed"] as! String)
                    self.usernameslost.append(self.userslost[post["userid"] as! String]!)
                    self.imageFileslost.append(post["imageFile"] as! PFFile)

                }
            }
        }

        // QUERY FOUND

        let queryfound = PFQuery(className: "Post")
        queryfound.whereKey("lostfound", equalTo: "found")
        queryfound.findObjectsInBackground { (objects, error) in

            if let posts = objects {

                for post in posts {

                    self.addressfound.append(post["address"] as! String)
                    self.breedfound.append(post["breed"] as! String)
                    self.usernamesfound.append(self.userslost[post["userid"] as! String]!) **--> ERROR IS HERE**
                    self.imageFilesfound.append(post["imageFile"] as! PFFile)

                }
            }

            }


        //TO SHOW DATA

        scrollView.delegate = self
        lostCollectionView.delegate = self
        lostCollectionView.dataSource = self
        foundCollectionView.delegate = self
        foundCollectionView.dataSource = self

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

// START OF EXTENSIONS FOR COLLECTION VIEWS

extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.lostCollectionView {
            return addresslost.count
            //DUDA #2
        }

        else {
            return addressfound.count
            //DUDA #2
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if collectionView == self.lostCollectionView {

            let cell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell

            //TIENES QUE IGUALAR LOS @IBOUTLETS DEL CELL (SPECIFICOS A LOST) Y IGUALARLOS CON EL ARRAY DE PARSE QUE PUEDES ENCONTRAR EN VARS ARRIBA

            cell.adressLostLabel.text = addresslost[indexPath.row]
            cell.breedLostLabel.text = breedlost[indexPath.row]
            cell.phoneLostLabel.text = phonelost[indexPath.row]

            imageFileslost[indexPath.row].getDataInBackground { (data, error) in

                if let imageData = data {

                    if let imageToDisplay = UIImage(data: imageData) {

                         cell.postedImage.image = imageToDisplay

                    }
                }
            }


            return cell
        }

        else {

            let cell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell

            cell.adressFoundLabel.text = addressfound[indexPath.row]
            cell.breedFoundLabel.text = breedfound[indexPath.row]
            cell.phoneFoundLabel.text = phonefound[indexPath.row]

            imageFilesfound[indexPath.row].getDataInBackground { (data, error) in

                if let imageData = data {

                    if let imageToDisplay = UIImage(data: imageData) {

                        cell.postedImage.image = imageToDisplay

                    }

                }

            }

            return cell
        }

    }

}

//SCROLL

extension  HomeViewController: UIScrollViewDelegate{

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView)
    }

}

可以在以下代码行中找到错误:
 self.usernamesfound.append(self.userslost[post["userid"] as! String]!)

最佳答案

请插入它,它应该可以工作:

self.usernamesfound.append(self.userslost [post [“userid”] as?String]?)

关于ios - 尝试将数据从Parse填充到CollectionView中时出现查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780377/

相关文章:

objective-c - 从 txt 文件中读取字符串到 iOS 中的 NSArrays

ios - 在编辑 UITextView 和触摸 TableView Controller 中键盘以外的任何地方时无法关闭键盘

swift - 如何查找 healthkit 开始使用 Swift 接收输入的日期

objective-c - Objective-C/Parse 云代码中的点赞数增加

javascript - 如何获得超过 1000 个结果 Parse

ios - 静音模式下的 UILocalNotification 声音

ios - 如何初始化 UIDocumentPickerViewController 注意到 "forOpeningContentTypes"(来自 doco)似乎在 IOS 中已被弃用?

swift - SKAction 运行 block 中的 SpriteKit 内存泄漏

ios - 在 Swift 中的设备上运行项目的缓慢延迟/延迟

node.js - 如何获取当前用户的sessionToken?