【问题标题】:Why am I getting the error: fatal error: unexpectedly found nil while unwrapping an Optional value?为什么我收到错误:致命错误:在展开可选值时意外发现 nil?
【发布时间】:2016-02-27 18:37:23
【问题描述】:

下面有一个 TableViewController,我试图用 Parse 的查询请求填充它。这个想法是调用(我已经检查并返回必要的信息)然后填充数组,然后我用它来填充 TableViewCells。这些单元格还有一个自定义类('TableViewCell')。

出于某种原因,'self.tableView.reloadData()' 肯定会导致崩溃。当我删除它时,它不会崩溃,但 tableviewcells 不会更新解析信息。有什么想法吗?

 import UIKit
 import Parse

 class AuctionViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")


} 

var capArray = [String]()
var imageDic = [String: [PFFile]]()
var priceArray = [Int]()


override func viewDidAppear(animated: Bool) {

    capArray.removeAll(keepCapacity: true)
    imageDic.removeAll(keepCapacity: true)
    priceArray.removeAll(keepCapacity: true)

    let query = PFQuery(className: "SellerObject")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if let objects = objects {
            for o in objects {
                if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
                let cap = o.objectForKey("caption") as? String
                    self.capArray.append(cap!)
                let imdic = o.objectForKey("imageFile") as? [PFFile]
                self.imageDic[cap!] = imdic
                let price = o.objectForKey("price") as? String
                let priceInt = Int(price!)
                self.priceArray.append(priceInt!)
                print(self.capArray)
                print(self.imageDic)
                print(self.priceArray)

            }
                self.tableView.reloadData()
            }

        }

    }


}


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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return capArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

    cell.captionLabel.text = self.capArray[indexPath.row]
    return cell
}

【问题讨论】:

  • 考虑使用自定义结构或类,而不是三个单独的数组。
  • 你设置了self.tableView.datasource吗?
  • 哪个变量为零?查看调试器。
  • 价格是否保证为整数值?

标签: ios swift uitableview parse-platform pfquery


【解决方案1】:

首先,您没有按类型检查 capimdicprice。并在循环中多次重新加载 tableView。替换

for o in objects {
            if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
            let cap = o.objectForKey("caption") as? String
                self.capArray.append(cap!)
            let imdic = o.objectForKey("imageFile") as? [PFFile]
            self.imageDic[cap!] = imdic
            let price = o.objectForKey("price") as? String
            let priceInt = Int(price!)
            self.priceArray.append(priceInt!)
            print(self.capArray)
            print(self.imageDic)
            print(self.priceArray)

        }
            self.tableView.reloadData()
        }

for o in objects {
           if let cap = o.objectForKey("caption") as? String,
           let imdic = o.objectForKey("imageFile") as? [PFFile],
           let priceInt = (o.objectForKey("price") as? String).flatMap({ Int($0))}) {
              self.capArray.append(cap)
              self.imageDic[cap] = imdic
              self.priceArray.append(priceInt)
              print(self.capArray)
              print(self.imageDic)
              print(self.priceArray)
           }
        }
self.tableView.reloadData()

另外,不要以这种方式使单元格出队。替换

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

      cell.captionLabel.text = self.capArray[indexPath.row]
      return cell
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? TableViewCell else {
        assertionFailure("cell for index-path:\(indexPath) not found")
        return UITableViewCell()
    }

    cell.captionLabel.text = self.capArray[indexPath.row]
    return cell
}

但我认为问题总是在 TableViewCell 类中? 例如,captionLabel 可能是 nil

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多