2017-10-18 60 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

相關問題