2016-02-24 40 views
1

您好,我有tableview中和裝載物品時表numberOFRowsInSection 0產生致命錯誤:意外發現零而展開的可選值斯威夫特的tableview numberOfRowsInSection回零致命錯誤可選值

我的代碼在這裏

var itemsList = [String]() 


internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
     self.itemsmessage.text = "Listed your last \(itemsList.count) items" 

    return itemsList.count 
} 

捐贈錯誤

fatal error: unexpectedly found nil while unwrapping an Optional value

當返回nil在self.itemsmessage.text行中給出此錯誤時。謝謝。

+0

我認爲當nubmerOfRowsInSection被調用itemList中數組是沒有得到分配。 –

+0

@AshishP。所以我需要哪些代碼更改? – SwiftDeveloper

+0

是'itemsList'真的是可選的嗎?如果是這樣,當你在應用程序的流程中創建它? –

回答

2

您的self.itemsmessage可能未連接到視圖控制器中的實際控件。檢查它的連接。爲了把安全的代碼放在那裏試試這個。

if let message = self.itemsmessage { message.text = "Listed your last \(itemsList.count) items" } else { print("Seems like your itemsmessage is not connected to a text label") }

+0

謝謝老兄工作。這是很好的解決方法嗎? – SwiftDeveloper

相關問題