2017-02-15 144 views
2

我是firebase的新手,爲我創建了一個Firebase結構,我想要做的基本上是列出所有用戶的FirebaseDB。在Swift中爲Firebase DataStructure創建類

我想快速創建一個相應的類到下面的firebase數據結構。

"users" : { 
    "A654tdC5uBPoKQzJnpZIpwOfsaJ3" : { 
     "groups" : { 
     "-KZ7fl3I4W7YWuGk6l9k" : true, 
     "-KclzmvvJF54cAg14P92" : true, 
     "-KclzpzrJOhCOWL5_jvw" : true, 
     "-Kcm33N1B_oiVYrQWh3n" : true, 
     "-Kcm3GfRaaGfztEBmflp" : true 
     }, 
     "location" : { 
     "coordinates" : [ 6.59086, 3.3914267 ], 
     "name" : "Oyebanjo Solarin Street, Lagos", 
     "visibility" : true 
     }, 
     "photoUrl" : "http://pbs.twimg.com/profile_images/511567946351923201/YBqqKc78_normal.jpeg", 
     "username" : "Kamiye", 
     "visibilityToSelectContacts" : { 
     "contacts" : { 
      "gFTCpzgSJDOsrVKWbAJp0Z1JFXp1" : true, 
      "rRxT6x87kgSjTwZfa7M8ZwzdFkC3" : true 
     }, 
     "visibility" : true 
     } 
    }, 

這就是我試圖和它不工作

class LContact { 

var visibliityToSelectedContacts: VisibilityToSelectContacts? 
var photoUrl: String? 
let username: String 
var location: LALocation? 
var groudId = [String]() 


init (value: [String: Any]) { 
    self.username = value["username"] as! String 
    self.photoUrl = value["photoUrl"] as? String 
    self.location = LALocation(value: value["coordinates"] as? [String:Any] ?? [:]) 
    self.groudId = (value["groups"] as? [String])! 
    } 
} 

class VisibilityToSelectContacts { 

var contacts = [String]() 
var visibility: Bool 

init(value: [String: Any]) { 
    self.contacts = [value["contacts"] as! String] 
    self.visibility = value["visibility"] as! Bool 
    } 
} 

struct LALocation { 

var long: Double 
var lat: Double 
var address: String! 
var visibility: Bool! 

init(long: Double, lat: Double, address: String?, visibility: Bool) { 
    self.long = long 
    self.lat = lat 
    self.address = address 
    self.visibility = visibility 
} 

init?(value: [String: Any]) { 
    guard let long = value["0"] as? Double, 
     let lat = value["1"] as? Double else {return nil} 
    self.long = long 
    self.lat = lat 
    self.address = value["name"] as? String 
    self.visibility = value["visibility"] as? Bool 
    } 
} 
+0

你能澄清你的問題?你想爲你的數據庫結構創建一個類模型 - 搜索註冊用戶?搜索數組在哪裏?碼?在Firebase中? *它不起作用*是什麼意思?你有錯誤嗎?提交的代碼似乎過於複雜,所以也許你可以簡化它 - 提供更多的細節(和更短的代碼)將幫助我們幫助你。另外,* A654tdC5uBPoKQzJnpZIpwOfsaJ3 *代表什麼? – Jay

+0

@Jay我已經發布了我的firebase結構的片段,我想要做的就是爲上面的片段創建一個類 – SimpiMind

+0

格式化遍佈各處,真的很難閱讀。我改變了我的評論。 – Jay

回答

3

讓我來幫你解決一些可能有用的代碼。這不是整個班級,但所有的概念都在那裏。我也是使用簡化的火力地堡結構,但同樣,這些概念是相同的:

這裏的火力地堡結構

users 
    a_uid 
    email: "some email" 
    groups 
     group_1: true 
     group_2: true 
    location 
     coords: "[52.5, 67.1]" 
     name: "Oyebanjo Solarin Street, Lagos" 
     visibility: true 
    username: "some username" 

首先有似乎是爲每個用戶多數民衆贊成在一個位置適合於作爲一個結構。

struct LocationStruct { 
     var coords: String? 
     var name: String? 
     var visibility: Bool? 
    } 

然後,我們在我們的用戶類中使用該結構。在這個例子中,我們傳入一個單一用戶的快照來初始化類並解構它以填充類變量。

class UserClass { 
    var email = "" 
    var username = "" 
    var groupsDict: [String: Any] 
    var loc = LocationStruct() 

    init(snap: FIRDataSnapshot) { 

     let userDict = snap.value as! [String: Any] 

     self.email = userDict["email"] as! String 
     self.username = userDict["username"] as! String 

     self.groupsDict = userDict["groups"] as! [String: Any] 

     let locationDict = userDict["location"] as! [String: Any] 
     self.loc.coords = locationDict["coords"] as? String 
     self.loc.name = locationDict["name"] as? String 
     self.loc.visibility = locationDict["visibility"] as? Bool 
    } 
} 

下面是一個用戶讀取和填充UserClass的

ref.child("users").child("a_uid") 
         .observeSingleEvent(of: .value, with: { snapshot in 

     let user = UserClass(snap: snapshot) 

     //this code is just to show the UserClass was populated. 
     print(user.email) 
     print(user.username) 

     for group in user.groupsDict { //iterate over groups 
      print(group) //and print each one 
     } 

     print(user.loc.coords!) //print the location data 
     print(user.loc.name!) 
     print(user.loc.visibility!) 
    }) 

只有在你的結構遺留的問題,那裏有一個visibilityToSelectContacts節點,其中有一個子節點接觸的代碼。

所以基本上在UserClass的UserDict中的高層次詞典將有一個孩子叫visibilityToSelectContacts然後有具有關鍵的子字典中的孩子接觸:的「gFTCpzgSJDOsrVKWbAJp0Z1JFXp1」值對:真。像這樣

userDict 
    visibilityToSelectContacts 
     contacts 
     contant_0: true 
     contant_1: true 

我會離開你得到那個結構敲定(提示:這是相同的設計模式)

+0

謝謝,它的工作! – SimpiMind

0

嘗試使用3D黨庫拆箱像this
也檢查你使用提取未來孩子如鍵:

LALocation(value: value["coordinates"] as? [String:Any] ?? [:]) 

在這裏你需要:

LALocation(value: value["location"] as? [String:Any] ?? [:]) 

和獲取數據後,不要忘記提取對象ID snapshot.key

+0

這與創建類匹配Firebase結構的問題有什麼關係?爲什麼在數據已經可用時添加第三方庫? – Jay

+0

我需要創建一個類,我不知道是否需要添加第三方。如果你能編輯我所嘗試過的東西,我將不勝感激。我對Firebase仍然陌生。 – SimpiMind