2017-10-18 73 views
-1

当我没有上传图像时,我点击了注册按钮,我就崩溃了。如果我上传照片而不填写剩余的textField,则会弹出提醒。如何在没有照片上传时获得相同的提醒?我得到的错误是当没有图像上传到Firebase时发生崩溃

致命错误:意外发现零而展开的可选值

@IBAction func signUpAction(_ sender: Any) { 
    let email = emailTextField.text!.lowercased() 
    let finalEmail = email.trimmingCharacters(in: .whitespacesAndNewlines) 
    let location = locationTextField.text! 
    let biography = biographyTextField.text! 
    let username = usernameTextField.text! 
    let password = passwordTextField.text! 
    let firstLastName = firstLastNameTextField.text! 
    let pictureData = UIImageJPEGRepresentation(self.userImageView.image!, 0.70) 

    if finalEmail.isEmpty || location.isEmpty || biography.isEmpty || username.isEmpty || password.isEmpty { 
     self.view.endEditing(true) 
     let alertController = UIAlertController(title: "OOPS", message: " You must fill all the fields", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) 
     present(alertController, animated: true, completion: nil) 

    }else { 
     self.view.endEditing(true) 
     authService.signUP(username: username, email: finalEmail, location: location, biography: biography, password: password, pictureData: pictureData as NSData!) 

    } 


} 

    var authService = AuthService() 



struct AuthService{ 

var dataBaseRef: DatabaseReference!{ 
    return Database.database().reference() 
} 
var storageRef: StorageReference!{ 
    return Storage.storage().reference() 
} 

func signUP(username: String, email: String, location: String, biography: String, password: String, pictureData: NSData!) { 

    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in 
     if error == nil{ 

      self.setUserInfo(user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData) 

     }else{ 
      print(error?.localizedDescription) 
     } 
    } 


} 

private func setUserInfo(user: User!, username: String, location: String, biography: String, password: String, pictureData: NSData!){ 

    let imagePath = "profileImage\(user.uid)/userPic.jpg" 
    let imageRef = storageRef.child(imagePath) 

let metaData = StorageMetadata() 
    metaData.contentType = "image/jpeg" 
    imageRef.putData(pictureData as Data, metadata: metaData){(newMetaData, error) 
     in 
     if error == nil{ 
      let changeRequest = user.createProfileChangeRequest() 
      changeRequest.displayName = username 
      if let photoURL = newMetaData!.downloadURL(){ 
       changeRequest.photoURL = photoURL 

      } 
      changeRequest.commitChanges(completion: { (error) in 
       if error == nil{ 

        self.saveUserInfo(user: user, username: username, location: location, biography: biography, password: password) 

        print("user info set") 

       }else{ 
        print(error?.localizedDescription) 
       } 
      }) 

     }else{ 
      print(error?.localizedDescription) 
     } 

    } 

} 

private func saveUserInfo(user: User!, username: String, location: String, biography: String, password: String){ 

    let userInfo = ["email": user.email!, "username": username, "location": location, "biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)] 

    let userRef = dataBaseRef.child("users").child(user.uid) 
    userRef.setValue(userInfo) { (error, ref) in 
     if error == nil{ 
      print("USER SAVED") 
      self.logIn(email: user.email!, password: password) 
     }else{ 
      print(error?.localizedDescription) 

     } 
    } 

} 

func logIn(email: String, password: String){ 

    Auth.auth().signIn(withEmail: email, password: password) { (user, error) in 
     if error == nil { 
      if let user = user { 
       print("\(user.displayName!) has been signed in") 

       let appDel : AppDelegate = UIApplication.shared.delegate as! AppDelegate 
       appDel.logUser() 

      }else{ 
       print(error?.localizedDescription) 

      } 

     } 
    } 
} 

}

+0

尝试添加这个条件'|| '||之后的pictureData == nil' password.isEmpty' in'if' statement.Also我会建议你在你的代码中进行修改,并尝试从你的代码中删除'!',并在你的代码中使用'if let'或'guard'语句来安全地打开你的可选项 – 3stud1ant3

+0

仍然在添加||之后得到相同的错误pictureData ==零我的if语句 – user8000557

回答

0

我想你需要补充一点:

var pictureData: NSData? //Try using Data in your code in swift 

if let imageView = self.userImageView.image { 
    pictureData = UIImageJPEGRepresentation(imageView, 0.70) 
} 

再加入这个陈述|| pictureData == nil|| password.isEmptyif陈述

+0

请不要回答重复的问题。相反,投票结束它们。 – JAL

+0

另外,这是一个不好的答案。为什么你在用'if let'分配'imageView'后强制展开'self.userImageView.image'? – JAL

+0

对不起,我不能删除我的答案,所以我更新了它并纠正了我所犯的错误,谢谢你的输入 – 3stud1ant3

相关问题