2015-11-18 26 views
0

目前我在我的單元格中有一個按鈕用作圖像,爲什麼按鈕而不是圖像,似乎使用按鈕保持圖像更清晰,但這不是這裏或那裏。試圖修復典型的發現零,而解包錯誤

if realDistance > 8000 
{ 
    self.tourist.text = "Tourist" 

    //crashing line 
    self.touristBackground.imageView?.image = UIImage(named: "tours-1") 

} 

這是我當前的代碼,註釋行是崩潰的行。我不知道爲什麼會崩潰,但不是上面的行。

確切的錯誤是致命錯誤:

unexpectedly found nil while unwrapping an Optional value

我想我可以嘗試的,如果讓,但似乎不起作用。然而,我可能做錯了。

編輯。它發生在這裏也

if lbuttonHidden == true 
{ 
    self.lls.text = String(self.lls.text!.toInt()! - 1) 

    //crashing line 
    if self.lls.text == "\(0)" 
    { 
     self.trophy.image = UIImage(named: "STAR2") 
    } 
} 
+0

那麼哪一位是零? – Wain

+0

來自@Anni S的答案應該可以做到。不過,我會很有興趣知道在你的班級中如何宣佈「touristBackground」。 – Eppilo

回答

1

試試這個...

if let imageView = self.touristBackground.imageView { 
imageView.image = UIImage(named: "tours-1") 
} 
else 
{ 
    print("Image view is nil!") 
} 
+0

這正是我所嘗試的,它仍然與相同的錯誤崩潰。 –

+0

在你的問題中添加類實現和崩潰日誌錯誤。 –

0

鑑於@Anni氏液不能正常工作(可選綁定),作爲測試的方式,取代

self.touristBackground.imageView?.image = UIImage(named: "tours-1") 

隨着

self.touristBackground.imageView?.image = UIImage() 

如果你的代碼不會崩潰,那麼錯誤是因爲(a)圖像不存在該名稱,或者由於某種原因不能使用AND(b)在你的類實現中,「圖像」被隱式解開(不能處理零沒有崩潰)。

相關問題