2014-10-08 62 views
-1

我在Xcode6.01和Swift中玩弄了Master-Detail iOS項目。我有一個簡單的數據輸入屏幕,它將swift字典對象添加到表格的Swift數據源數組中。Swift iOS - 將字典對象傳遞到詳細視圖

數據輸入和表視圖工作正常。點擊單元格時遇到此問題,從數組中提取字典對象並嘗試將該對象傳遞給詳細視圖。

詳細信息視圖中的configureView()函數引發異常。下面的代碼:

func configureView() { 

if let dictObject:Dictionary = detailItem as? Dictionary<String,String> { 

    if var strName = dictObject["name"] { 

    // prints fine: 
    println(strName) 

    // fatal error 
    self.detailDescriptionLabel.text = strName 

    // debug output: 
    // prints the string in strName (no mention of optional) 
    // assigning to label: 
    // fatal error: unexpectedly found nil while unwrapping an Optional value 

    } 

} 

} 

似乎很奇怪的是,的println()語句與變量則strName沒有問題,也沒有它提到作爲一個可選的輸出面板。只要它試圖將字符串分配給標籤,砰 - 崩潰。

+0

可能是錯誤與'self.detailDescriptionLabel'有關,可能是可選的嗎? – zisoft 2014-10-08 11:51:38

回答

0

找到答案我的自我。字典對象不是零,而是視圖中的標籤!

與IF LET構建它的工作原理是這樣的標籤調整代碼後:

func configureView() { 

if let dictObject:Dictionary = detailItem as? Dictionary<String,String> { 

    if let dLabel = detailDescriptionLabel { 

    dLabel.text = dictObject["name"] 

    } 

    if let eLabel = emailLabel { 

    eLabel.text = dictObject["email"] 
    } 

} 
} 

我必須承認,雖然,我不知道爲什麼這是必要的。我認爲故事板會啓動標籤。有問題的標籤是常規IBOutlets:

@IBOutlet weak var detailDescriptionLabel: UILabel! 

@IBOutlet weak var emailLabel: UILabel! 
相關問題