gpt4 book ai didi

ios - 我无法获取 snapshot.key swift ios

转载 作者:行者123 更新时间:2023-11-28 06:20:40 25 4
gpt4 key购买 nike

此代码获取值 snap(电子邮件、姓名)。我需要获取 key (RxLV2i1)。怎么改代码?

FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
let user = Subject()
user.setValuesForKeys(dictionary)
self.subjects.append(user)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
} }, withCancel: nil)

例如:

Snap (RxLV2i1) {
email = "Test1@gmail.com";
name = "Test1 test1";
}

当我从 snapshot.value 更改为 snapshot.key - 显示消息:

Cast from 'String?!' to unrelated type '[[String : AnyObject]]' always fails

最佳答案

如果你想获取快照 key ,就这样做

 let key = snapshot.key
print(snapshot.key)

在您的代码中....

FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]{
let user = Subject()
user.setValuesForKeys(dictionary)

// Also if you add another variable called 'id' to your user object, you can set that as well to 'user.id = snapshot.key'
let key = snapshot.key
print(snapshot.key)

self.subjects.append(user)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
} }, withCancel: nil)

关于ios - 我无法获取 snapshot.key swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616092/

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