1

I'm trying to implement a ViewController feature to edit and save Profiles with the help of Firebase but, I get the error: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value when using this code below:

@IBAction func saveButton(_ sender: Any) {
    var data = Data()
    data = profileImage.image!.jpegData(compressionQuality: 0.1)!
    DataService.dataService.SaveProfile(username: usernameTextfield.text!, email: emailTextfield.text!, data: data as NSData)
}

1 Answers1

2

You may use following ( if let statements ) to check optional value errors.

@IBAction func saveButton(_ sender: Any) {
    var data = Data()
    if let image = profileImage.image {
        if let data = image.jpegData(compressionQuality: 0.1) {
            DataService.dataService.SaveProfile(username: usernameTextfield.text!, email: emailTextfield.text!, data: data as NSData)
        }
    }
}
SHS
  • 1,414
  • 4
  • 26
  • 43