我怎样才能从 child 获取数组并在 swift 中从 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
}
我有一个快速问题,我在这里找不到。
我有一个结构可以保存我的 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
}