[英]how can i get array from child and save in struct from firebase in swift
I have a fast question that i not found exactly here.我有一个快速的问题,我在这里找不到。
I have a struct that save all data from my firebase user.我有一个结构可以保存我的 firebase 用户的所有数据。 The problem is that my user have an dynamic list, always can be 0, or have 3 or more lines of type [String:Int], in resume i think a_trips is [[String:Int]]
问题是我的用户有一个动态列表,总是可以为 0,或者有 3 行或更多行类型 [String:Int],在简历中我认为 a_trips 是 [[String:Int]]
my actual struct:我的实际结构:
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
}
} }
example of user in firebase: firebase 中的用户示例:
id:{
uid:{
fullname: "name",
uid: "121948349238",
location: "example location",
a_trips: {
"trip1": {
id: 1432542543 (int)
},
"trip2": {
id: 1523524654 (int)
}
...
}
}
}
or... sometimes can be:或者......有时可以是:
id:{
uid:{
fullname: "name",
uid: "121948349238",
location: "example location",
a_trips: {
// nil
}
}
}
Solved.解决了。 Thanks so much all.
非常感谢大家。 I could find the solution
我能找到解决方案
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.