當我沒有上傳圖像時,我點擊了註冊按鈕,我就崩潰了。如果我上傳照片而不填寫剩餘的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)
}
}
}
}
}
嘗試添加這個條件'|| '||之後的pictureData == nil' password.isEmpty' in'if' statement.Also我會建議你在你的代碼中進行修改,並嘗試從你的代碼中刪除'!',並在你的代碼中使用'if let'或'guard'語句來安全地打開你的可選項 – 3stud1ant3
仍然在添加||之後得到相同的錯誤pictureData ==零我的if語句 – user8000557