gpt4 book ai didi

swift - 从 Firestore 中获取用户配置文件的数据(swift、iOS、Xcode、firebase/firestore)

转载 作者:行者123 更新时间:2023-12-05 02:05:35 25 4
gpt4 key购买 nike

我在这里有点新手,所以请客气。我是一名前空军飞行员,目前在法学院学习,所以编码不是我的全职工作......但我正在努力边学边学(以及帮助我​​的 children 学习)。

我正在为我的 iOS 应用开发配置文件页面。我已经相当广泛地浏览了 firebase 文档,但它只是没有详细说明我在这里要做什么。我也在这个网站上搜索过,试图找到答案……我发现了一些真正有用的东西,但我觉得有些东西不太对劲。我之前发布过这个,但我删除了,因为我没有收到任何有用的输入。

我想做的是通过标签显示用户的数据(名字、姓氏、电话、地址等)。代码(在下面提供)用于显示用户 ID 和电子邮件...我认为这是因为它是从身份验证中提取的,而不是从“用户”集合中提取的。此代码试图从用户集合中的相应文档中提取其余用户数据。

这里是 viewController 的完整代码。我已经尝试了很多次但都失败了,以至于我真的在最后一根稻草上......很难坚持!请帮忙!

我的猜测是 firstName 变量有问题...无论是前面的数据库快照有问题,还是变量的实际编码有问题。但话又说回来……我不知道自己在做什么……所以也许我对问题的看法还很遥远。

//  ClientDataViewController.swift

import UIKit
import Firebase
import FirebaseAuth
import FirebaseFirestore



class ClientDataViewController: UIViewController {





@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var streetLabel: UILabel!
@IBOutlet weak var street2Label: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var zipLabel: UILabel!
@IBOutlet weak var attorneyLabel: UILabel!
@IBOutlet weak var updateButton: UIButton!
@IBOutlet weak var passwordButton: UIButton!
@IBOutlet weak var uidLabel: UILabel!




let id = Auth.auth().currentUser!.uid
let email = Auth.auth().currentUser!.email



// MARK: Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

self.uidLabel.text = id
self.emailLabel.text = email

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) // call super

getName { (name) in
if let name = name {
self.firstNameLabel.text = name
print("great success")
}
}
}

// MARK: Methods

func getName(completion: @escaping (_ name: String?) -> Void) {
let uid = "dL27eCBT70C4hURGqV7P"
let docRef = Firestore.firestore().collection("users").document(uid)

docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
completion("put the first name data here after we figure out what's in the doc")
}
}
}

最佳答案

以下解决您的问题。但是,我建议不要将 idemail 声明为强制展开的实例属性;它们甚至不需要是实例属性,更不用说强制解包了。在使用它们的值之前总是安全地解包可选值,尤其是这些授权属性,因为如果用户没有登录或在你下面注销(例如过期的 token ),应用程序会在这里崩溃,就像飞行的飞机一样,崩溃是总是要避免。

class ClientDataViewController: UIViewController {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
@IBOutlet weak var emailLabel: UILabel!
@IBOutlet weak var phoneLabel: UILabel!
@IBOutlet weak var streetLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var zipLabel: UILabel!
@IBOutlet weak var attorneyLabel: UILabel!
@IBOutlet weak var updateButton: UIButton!
@IBOutlet weak var passwordButton: UIButton!
@IBOutlet weak var uidLabel: UILabel!

let id = Auth.auth().currentUser!.uid
let email = Auth.auth().currentUser!.email

// MARK: Lifecycle

override func viewDidLoad() {
super.viewDidLoad()

self.uidLabel.text = id
self.emailLabel.text = email
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) // call super

getName { (name) in
if let name = name {
self.firstNameLabel.text = name
print("great success")
}
}
}

// MARK: Methods

func getName(completion: @escaping (_ name: String?) -> Void) {
guard let uid = Auth.auth().currentUser?.uid else { // safely unwrap the uid; avoid force unwrapping with !
completion(nil) // user is not logged in; return nil
return
}
Firestore.firestore().collection("users").document(uid).getDocument { (docSnapshot, error) in
if let doc = docSnapshot {
if let name = doc.get("firstName") as? String {
completion(name) // success; return name
} else {
print("error getting field")
completion(nil) // error getting field; return nil
}
} else {
if let error = error {
print(error)
}
completion(nil) // error getting document; return nil
}
}
}
}

感谢您的服务!希望你能驾驶 B1-B。

关于swift - 从 Firestore 中获取用户配置文件的数据(swift、iOS、Xcode、firebase/firestore),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63422675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com