嘗試在swift中開發登錄頁面,然后我想驗證某些條件以獲得錯誤回傳。
從print(Error.localizeDescription)中我得到了一些回報
從print(Error.localizeDescription)中,我得到了一些回傳,如
- 密碼無效或用戶沒有密碼 。
- 沒有對應于這個識別符號的用戶記錄。
如何基于Error.localizeDescription來驗證這個條件?
Auth.auth().signIn(withEmail: email, password: pass, completion: {[weak self] result, Error in >。
guard let StrongSelf = self else{
return
}
if let Error = Error {
print(Error.localizedDescription)
}
guard Error == nil else{
//在這里有驗證回傳錯誤(例如,錯誤的密碼,帳戶不存在)
self?.reasonLabel.isHidden=false。
self?.reasonLabel.text=Error
return。
}
self!.checkinfo()
})
uj5u.com熱心網友回復:
首先,你不應該用大寫字母來宣告任何屬性,如果需要的話,可以使用小寫字母或駱駝字母。 其次,如果你使用[weak self]并制定了保護性的let條件,那么請使用強的條件來防止保留回圈,并盡量不要使用強制解包。
Auth.Auth().signIn(withEmail: email, password: pass, completion: {[weak self] result, error in
guard let self = self else { return }
if let error = error {
print(error.localizedDescription)
let alert = UIAlertController.configureAlertController(error.localizedDescription)。
self.present(alert, animated: true)
return。
}
//你的邏輯。
/* 或
如果讓error = error {
print(error.localizedDescription)
UIAlertController.showAlert(error.localizedDescription)
}else{
// 你的邏輯
}
*/
/* or
守護 讓結果 = 結果 否則 {
showAlert(error?.localizedDescription?? "未知錯誤")
回傳
}
// 你的邏輯
*/
/*
或其他,但要避免強行解包
*/
})
注意你的帶有錯誤檢查的if let陳述句不會阻止你整個代碼的執行,self!.checkinfo()將被執行,return在if let error...的最后將告訴你的方法,他需要停止
extension UIAlertController {
static func configureAlertController(with title: String = "注意", message: String) -> (UIAlertController){
let alertController = UIAlertController(title: 標題, message: 訊息, preferredStyle: .alert)
let action = UIAlertAction(title: "ОК", style: .default) {(action) in}
alertController.addAction(action)
return alertController
}
}
uj5u.com熱心網友回復:
如果你想根據Firebase認證引發的錯誤采取特定的行動,決議Error.localizeDescription并不是最好的方法。相反,請再次檢查你在完成處理程式中得到的NSError物件的錯誤code,即處理錯誤的檔案中顯示的值。
例如(基于此repo:
switch error {
case let .some(error as NSError)
where UInt(error.code) == FUIAuthErrorCode.userCancelledSignIn.rawValue:
print("用戶取消了登錄")
case .none:
if let user = authDataResult? .user {
signed(in: user)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/323152.html
標籤:
上一篇:如何根據前一行的值來更新一個值
