簡體   English   中英

我如何從 child 獲取數組並將 firebase 保存在 swift 的結構中

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM