xcode - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

标签 xcode swift firebase option-type

我是代码新手,我不知道如何在这里删除我的可选值。我在某处读到这可能是我的问题。任何帮助都会很棒!

我一直在关注这个教程:https://www.youtube.com/watch?v=Qyy8pJd4IWU

@IBAction func dropPhoto(sender: AnyObject) {

    presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [NSObject : AnyObject]?) {

    self.dismissViewControllerAnimated(true, completion: nil)

    let thumbnail = image.resizedImageWithContentMode(UIViewContentMode.ScaleAspectFit, bounds: CGSizeMake(400, 400), interpolationQuality: CGInterpolationQuality.High)
    let imgData = UIImagePNGRepresentation(thumbnail)
    let base64EncodedImage = imgData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())

    let uniqueReference = firebase?.childByAutoId()
    uniqueReference!.setValue(base64EncodedImage)

    let key = uniqueReference?.key
    _ = mapView.userLocation.location
    geofire!.setLocation(mapView.userLocation.location,forKey: key)

}

最佳答案

每当您看到此错误时,请查找“!”

这里有两行包含强制展开

geofire!.setLocation(mapView.userLocation.location,forKey: key)

 uniqueReference!.setValue(base64EncodedImage)

您应该能够通过简单地替换 !与一个? ,例如

geofire?.setLocation(mapView.userLocation.location,forKey: key)

只有当 geoFire 是一个真实值时,才会调用 setLocation,否则如果你还想处理 nil 情况,快速的方法是:

if let geoFire = geoFire {
      geoFire.setLocation(mapView.userLocation.location, forKey: key)
}
else{
     *do something*
}

关于xcode - fatal error : unexpectedly found nil while unwrapping an Optional value (lldb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35885678/

相关文章:

xcode - Swift 版本和更新 Swift 版本

xcode - xcode 项目中事件可执行文件的目的是什么?

xcode - 如何在 xcode 4.5.1 上使用 WebView

objective-c - 桥接头文件中找不到文件错误

swift - 在 swift 函数中返回 nil

firebase - 当应用程序在后台时,通知无法正确显示(React Native)

javascript - 当向我显示此错误 "Cannot read properties of undefined (reading ' _internalPath') 时,我尝试使用 nodejs 和 firestore 中的过滤器获取数据”

reactjs - 如何从扫描的二维码中检索数据?

在单独文件中定义的 Swift 结构是否需要初始化程序?

ios - 我希望状态栏在全屏视频播放期间可见