ios - unexpectedly found nil while unwrapping an Optional value?
PHPz
PHPz 2017-04-17 17:36:59
0
3
615

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)

PHPz
PHPz

学习是最好的投资!

全員に返信(3)
巴扎黑

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

いいねを押す +0
Peter_Zhu

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

いいねを押す +0
Peter_Zhu

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

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート