swift函数值保持返回零错误

时间:2015-03-22 01:12:26

标签: swift

写一个简单的swift应用程序练习,它是一个塔罗牌iphone应用程序(按一个按钮和报价/图像更改)。我的报价改变了工作。但图像似乎没有改变。此功能是控制器中的位置。 (它没有检索财产)

//Property referencing the label in the view
@IBOutlet weak var lblAnswers: UILabel!

@IBOutlet weak var imgCard: UIImageView!



@IBAction func askTheOracle(sender:UIButton) {

    // Retrieve a random message from the oracle
    lblAnswers?.text = model.respond()

    // Change the image in the UIImageView to the currently selected card
    imgCard?.image = UIImage(named:model.currentCardImageName)
}

调试器错误:“致命错误:在解包可选值时意外发现nil” imgCard是零

请参阅:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试这样做以确保在尝试将imgCard?.image设置为它之前,UIImage(名称:model.currentCardImageName)不是nil。

@IBAction func askTheOracle(sender:UIButton) {

    // Retrieve a random message from the oracle
    lblAnswers?.text = model.respond()

    // Change the image in the UIImageView to the currently selected card
    if let newImage = UIImage(named:model.currentCardImageName)
    {
          imgCard?.image = newImage
    }
}

如果仍然收到错误,请确保imgCard已正确连接到您的故事板。