【问题标题】:Error while adding data in userID in Firebase Swift 3在 Firebase Swift 3 中的 userID 中添加数据时出错
【发布时间】:2017-10-23 02:12:46
【问题描述】:

我正在尝试通过注册用户在 Firebase 中按相应的用户 ID 添加数据,但它给了我错误“unexpectedly found nil while unwrapping an optional value”现在我不知道这是怎么回事。但是当我使用代码而不分别在ref 中添加userID 时,数据添加成功。但是当我在ref 之后添加userID 时出现错误。


注册

  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])

【问题讨论】:

  • 请采纳其中一个答案
  • @VladPulichev 仍在寻找答案,因为数据没有像我预期的那样存储在 UID 中
  • ATM 怎么了?
  • 我已经完成了。对不起。我已经写了,你需要做什么。使用您的注册码提出另一个问题。祝你好运。
  • 还有一件事。试着首先考虑自己..

标签: ios firebase swift3 firebase-realtime-database firebase-authentication


【解决方案1】:

您需要了解错误。您正在强制解开 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
 }

【讨论】:

  • 它保存了以前登录的 i.d 的数据,我已经完成了注销,但是它在两个 i.d 中都没有保存任何内容,但显示它已经完成
  • 是的,它做它应该做的。如果用户未登录,您需要让他登录else 块或@vlad 建议的其他方式。
  • 亲爱的 Rahul 数据仍未被存储,我认为它是可选的吗?有什么帮助吗??
【解决方案2】:

您的用户未登录 => 解包错误。你需要有类似的东西:

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 中。它会将您的初始视图控制器更改为登录页面,如果用户未登录。

使用此代码,您可以强制展开。

希望对你有帮助

【讨论】:

  • 我已经定义了,但是MainTabBarController
  • @Xcodian 你的主控制器与你的逻辑开始
  • 我在开始时有导航控制器,然后是登录页面
  • @Xcodian 登录后您要访问的控制器
  • 现在可以了,我已将tabbarcontroler 定义为Homecontroller,即登录后会出现第一个屏幕
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多