検索

ホームページ  >  に質問  >  本文

ios - unexpectedly found nil while unwrapping an Optional value?

import UIKit

class DetailTableViewController: UITableViewController {

var restaurant: Restaurant!

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = UIImage(named: restaurant.image)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCellWithIdentifier("DetailCell", forIndexPath: indexPath) as! DetailTableViewCell
    
    switch indexPath.row{
    case 0:
        cell.fieldLabel.text = "Restaurant Name"
        cell.valueLabel.text = restaurant.name
    case 1:
        cell.fieldLabel.text = "Restaurant Type"
        cell.valueLabel.text = restaurant.type
    case 2:
        cell.fieldLabel.text = "Restaurant Location"
        cell.fieldLabel.text = restaurant.location
    case 3:
        cell.fieldLabel.text = "Visted?"
        cell.fieldLabel.text = restaurant.isVisted ? "Yes":"No"
    default:
        cell.fieldLabel.text = ""
        cell.valueLabel.text = "" 
    }
    return cell
}

}

错误信息: fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

PHPzPHPz2887日前611

全員に返信(3)返信します

  • 巴扎黑

    巴扎黑2017-04-17 17:38:59

    オブジェクト restaurant は初期化されていないため、後で tableView:cellForRowAtIndexPathrestaurant オブジェクトを使用する場合、ラップ解除時にエラーが報告されます。 null

    返事
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:38:59

    私は Swift を学んだことがありませんが、oc の経験とエラー メッセージに基づいて、再利用プールにセルがなく、再利用プールからセルを取得するためだけに記述したのではないかと大胆に推測します。作成してください、わかりません、そうですか?

    返事
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:38:59

    エラー変換: オプションの値が解凍されると、null 値であることがわかります。これは、ある程度 null ポインターの問題と同等です。
    どの値が初期化されていないのかを確認できます

    返事
    0
  • キャンセル返事