2017-10-19 106 views
0

我懷疑我的代碼有問題,我無法確定我在這裏做錯了什麼。錯誤在這裏:self.setUserInfo(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData)這是我的signUp功能。無法轉換'User?'類型的值到期望的參數類型'用戶!'

無法轉換'User?'類型的值到期望的參數類型'用戶!' enter image description here

func signUP(firstLastName: String, 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(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password, pictureData: pictureData) 

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


} 

private func setUserInfo(firstLastName: String, 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(firstLastName: firstLastName, user: user, username: username, location: location, biography: biography, password: password) 

        print("user info set") 

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

     }else{ 
      print(error?.localizedDescription) 
     } 

    } 

} 
+0

展會實現Auth.auth()的createUser(withEmail的':'方法 – D4ttatraya

+0

什麼是您的用戶類的樣子 – Torewin

回答

-1

在setUserInfo你用!「」用戶設置參數的值展開的方法,但在你的權威性。完成塊看起來這個值是可選的。

這裏是你如何解決這個問題:

Auth.auth().createUser(withEmail: email, password: password) { [weak self] (user, error) in 

    guard let unwrappedError = error, 
      let unwrappedUser = user, 
      let strongSelf = self 
    else { 
     print(error?.localizedDescription) 
     return 
    } 

    strongSelf.setUserInfo(firstLastName: firstLastName, user: unwrappedUser, username: username, location: location, biography: biography, password: password, pictureData: pictureData) 
} 

編輯:我添加了一個strongSelf模式和使用的後衛聲明,因爲你明確地保留自己在這裏,(一種沒有沒有網絡回調倒閉!)和guard警報是在這種情況下處理有效性檢查的首選方式,因爲它使得它更清楚你的意圖是什麼。

+0

這給了我從setUserInfo同樣的錯誤 – user8000557

+0

刪除宣佈用戶參數 – Woof

+0

由此帶來重生嗎?「!」 (圖片提供) – user8000557

相關問題