即使我检查self.photoImageView.image != nil
是否正确,但当我在倒数第二行尝试fatal error: unexpectedly found nil while unwrapping an Optional value
时,我仍然会收到applyBlurEffect
错误。
你知道如何调和吗?
if (output?.getAccel() == true){
if (output?.getImage() != nil){
if (self.photoImageView.image != nil){
println(photoImageView.image)
var blurredImage = self.applyBlurEffect(self.photoImageView.image!)
self.photoImageView.image = blurredImage
}
对于上下文,我有一个photoImageView
,当一个"加速度计按钮"插入photoImageView
,此功能会拍摄该图像,使其模糊,并将图像更新为模糊图像。
当我打印photoImageView.image
时,它会返回
Optional(<UIImage: 0x174087d50> size {1340, 1020} orientation 0 scale 1.000000)
。可能存在问题,但我需要一些帮助来解决它。
答案 0 :(得分:2)
在Swift中,您必须使用可选绑定来确保可选项不是nil。在这种情况下,你应该这样做:
if let image = self.photoImageView.image {
//image is set properly, you can go ahead
} else {
//your image is nil
}
这是Swift中一个非常重要的概念,因此您可以阅读更多here。
更新:正如@rdelmar所述,可选绑定不是强制性的,检查nil
也应该足够了。我个人更喜欢使用可选绑定。它的一个好处是multiple optional binding
而不是检查nil的所有选项:
if let constantName = someOptional, anotherConstantName = someOtherOptional {
statements
}