2014-10-08 59 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! 
相关问题