![](/img/trans.png)
[英]How to get array of data from a child node in Swift and Firebase?
[英]how can i get array from child and save in struct from firebase in swift
我有一个快速的问题,我在这里找不到。
我有一个结构可以保存我的 firebase 用户的所有数据。 问题是我的用户有一个动态列表,总是可以为 0,或者有 3 行或更多行类型 [String:Int],在简历中我认为 a_trips 是 [[String:Int]]
我的实际结构:
struct User {
let fullname: String
let location: String
let uid: String
let a_trips: ?
init(uid: String, dictionary: [String: Any]) {
self.uid = uid
self.fullname = dictionary["fullname"] as? String ?? ""
self.location = dictionary["location"] as? String ?? ""
// i dont know to get array of array of a_trips here
}
}
firebase 中的用户示例:
id:{
uid:{
fullname: "name",
uid: "121948349238",
location: "example location",
a_trips: {
"trip1": {
id: 1432542543 (int)
},
"trip2": {
id: 1523524654 (int)
}
...
}
}
}
或者......有时可以是:
id:{
uid:{
fullname: "name",
uid: "121948349238",
location: "example location",
a_trips: {
// nil
}
}
}
解决了。 非常感谢大家。 我能找到解决方案
struct User {
let fullname: String
let location: String
let uid: String
// i finally create a simple array
var tripList: [Int]
init(uid: String, dictionary: [String: Any]) {
self.uid = uid
self.fullname = dictionary["fullname"] as? String ?? ""
self.location = dictionary["location"] as? String ?? ""
var arrayTripsAux = [Int]()
if let a_trips = dictionary["a_trips"] as? [String: [String:Int]]{
for t in a_trips.values{
arrayTripsAux.append(t["id"] ?? 0)
}
} else {
arrayTripsAux.append(0)
}
// return childs in array
self.tripList = arrayTripsAux
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.