在Swift中为Firebase DataStructure创建类

时间:2017-02-15 22:52:17

标签: ios swift firebase firebase-realtime-database

我是firebase的新手,为我创建了一个Firebase结构,基本上我要做的就是列出FirebaseDB的所有用户。

我想在swift中创建一个对应的类到下面的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
   }
}

2 个答案:

答案 0 :(得分:4)

让我告诉你一些可能有用的代码。它不是整个班级,但所有的概念都在那里。我也使用简化的Firebase结构,但同样,概念是相同的:

这是Firebase结构

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的子项,然后它有一个子联系人,它有一个key的子词典:> gFTCpzgSJDOsrVKWbAJp0Z1JFXp1" :是的。喜欢这个

userDict
   visibilityToSelectContacts
      contacts
        contant_0: true
        contant_1: true

我会让你把这个结构搞定(提示:它的设计模式相同)

答案 1 :(得分:0)

尝试使用3d派对库进行取消装箱,例如this 还要检查用于提取未来孩子的密钥,例如:

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

这里你需要:

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

并且在获取数据后不要忘记提取对象标识snapshot.key