gpt4 book ai didi

swift - 传递 Firebase 数据库引用数据

转载 作者:行者123 更新时间:2023-11-28 05:56:48 25 4
gpt4 key购买 nike

我希望使用 segue 传递一个数组,该数组包含从 Firebase 从一个 Controller 提取的用户信息到另一个 Controller 。当一切都在 TableView 中时,我能够做到这一点,但当它在常规 Controller View 中时,我无法做到。有人可以帮忙吗?

View Controller

var userArray = [User]()

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showGuestView" {
let guestVC = segue.destination as! GuestUserViewController
guestVC.ref = userArray.ref //this would work using userArray[indexPath.row].ref if it was a tableview
//ref represents DatabaseReference?
}
}

DatabaseClass.fetchUser() { (user) in
if let user = user {
self.userArray.append(user)
}

数据库类

func fetchUser(completion: @escaping (User?)->()){

let currentUser = Auth.auth().currentUser!

let postRef = Database.database().reference().child("getinfo").child(currentUser.uid)

postRef.observe(.value, with: { (snapshot) in
for childSnapshot in snapshot.children.allObjects as! [DataSnapshot] {
let request = childSnapshot.key
let userRef = self.databaseRef.child("users").child(request)

userRef.observeSingleEvent(of: .value, with: { (currentUser) in

let user: User = User(snapshot: currentUser)
completion(user)
})
}
})
}

用户结构

struct User {

var firstname: String!
var uid: String!
var ref: DatabaseReference!

init(firstname: String, uid: String){
self.firstname = firstname
self.uid = uid
self.ref = Database.database().reference()
}

init(snapshot: DataSnapshot){

if let snap = snapshot.value as? [String:Any] {
self.firstname = snap["firstname"] as! String
self.uid = snap["uid"] as! String
}
self.ref = snapshot.ref
}

func toAnyObject() -> [String: Any]{
return ["firstname":self.firstname, "uid":self.uid]
}
}

最佳答案

我可以看到 userArray 是一个用户数组,因此您不能使用 userArray.ref 因为该数组没有像 ref< 这样的任何属性 在其定义中。在 TableView 中,它可以正常工作,因为您提取了一个 user 对象,然后传递了 ref

在传递给访客 View Controller 之前尝试获取选定的用户实例。

let user =  userArray[selected user index];
guestVC.ref = user.ref

关于swift - 传递 Firebase 数据库引用数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51278265/

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