0

將在用戶ID數據我想在火力地堡各自的用戶ID的註冊用戶添加數據,但它給了我錯誤「unexpectedly found nil while unwrapping an optional value」現在我不知道問題出在哪兒。但是當我分別使用代碼userID而不是ref時,數據被成功添加。但是當我添加userID以下ref然後出現錯誤。錯誤而在火力地堡斯威夫特3


註冊

let userID = FIRAuth.auth()?.currentUser?.uid 


    ref.child("user_registration").child(userID!).setValue(["username": self.fullName.text, "email": self.emailTextField.text,"contact": self.numberText.text, "city": self.myCity.text, "state": self.countryText.text, "gender": genderGroup, "blood": bloodGroup]) 

enter image description here

+0

接受的答案請 –

+0

@VladPulichev還在尋找一個答案,因爲數據不被存儲在一個UID如我所料 –

+0

哪些錯誤ATM? –

回答

2

你需要理解的錯誤。您正在強制解開userID,這不是一個好主意,因爲當您調用此API時,用戶可能會或可能不會登錄。以下更改將解決您的問題。

if let userID = FIRAuth.auth()?.currentUser?.uid { 
    ref.child("user_registration").child(userID).setValue(["username": self.fullName.text, "email": self.emailTextField.text,"contact": self.numberText.text, "city": self.myCity.text, "state": self.countryText.text, "gender": genderGroup, "blood": bloodGroup] 
} else { 
    // ask the user to login in 
    // present your login view controller 
} 
+0

它保存了以前登錄的i.d的數據,我已經完成註銷,但它不會在i.d中保存任何內容,但顯示它已完成 –

+1

是的,它完全按照它的設想。如果用戶未登錄,您需要請求他在'else'塊中登錄,或者另一種方式是由@vlad建議。 – Rahul

+0

親愛的Rahul數據仍然沒有被存儲,我認爲它是可選的嗎?任何幫助? –

1

您的用戶未登錄=>展開錯誤。你需要有這樣的:

func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // FireBase init part 
    FIRApp.configure() 
    FIRDatabase.database().persistenceEnabled = false 

    self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 

    // Setting initial viewController for user loggedIn? 
    if(FIRAuth.auth()?.currentUser != nil) { 
    self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") 
    } else { 
    self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "LoginPage") 
    } 

    return true 

}

AppDelegate。它會改變你的初始視圖控制器到登錄頁面,如果用戶沒有登錄。

有了這個代碼,你可以強制展開。

希望它可以幫助

+0

我已經定義了它,但是你的主控制器的邏輯 –

+0

@Xcodian仍然不清楚? –

+0

開始的'MainTabBarController' –

相關問題