简体   繁体   English

我如何从 child 获取数组并将 firebase 保存在 swift 的结构中

[英]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.

相关问题 如何从 Swift 和 Firebase 中的子节点获取数据数组? - How to get array of data from a child node in Swift and Firebase? 如何从 Firebase 远程配置中获取数组 json 的值?(Swift) - How can I get array json value from Firebase remote-config ?(Swift) 我无法获取数组并从 Firebase 存储中放入另一个数组 swift - I can't get array and put in the another array swift from Firebase storage 如何在父函数中获取子结构名称? - how can i get child struct name in parent func? 如何从 Firebase 数据库中获取孩子的数据? - How to get data from a child in Firebase Database? android工作室中firebase的孩子如何获取? - How to get the child from firebase in android studio? 我在数组中遇到问题我从 firebase (firestore) 获取数据,我想显示数据数组我该怎么做? - I face a problem in the array I get the data from firebase (firestore) and I want to show array of data how can i do? 我可以在不知道一个子路径的情况下从 Firebase 存储中获取文件吗? - Can I get file from Firebase Storage while not knowing one child path? 如何使用 flutter 中的子值从 firebase 获取数据? - How to get data from firebase using child value in flutter? 如何在 flutter 中实时获取 firebase 数据库中孩子的价值? - how can i get the value of a child in firebase database realtime in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM