从Firebase数据库检索元素数组

时间:2018-08-21 05:45:57

标签: ios swift firebase firebase-realtime-database

所以我暂时失去了判断力。我更改了保存事件的数据库。我希望事件具有标签和主机。我已经成功地按照我认为的外观构建了数据库,但我很难更改模型来检索此元素数组。

这是我目前的型号

import Foundation
import FirebaseDatabase.FIRDataSnapshot

@objc(Event)
class Event:NSObject{
    var key: String?
    let currentEventName: String
    let currentEventImage: String
    let currentEventPromo: String?
    let currentEventDescription: String
    //nested properties
    let currentEventStreetAddress: String
    let currentEventCity: String
    let currentEventState: String
    let currentEventDate: String?
    let currentEventEndDate: String?
    let currentEventTime: String?
    let currentEventEndTime: String?
    let eventPrice: String
    let endTime: Date
    let startTime: Date
    let currentEventZip: Int
    var category: String
    //nested properties stop
    var currentAttendCount: Int
    var isAttending = false
    var eventDictionary: [String: Any]{


        let dateDict = ["start:date":currentEventDate, "start:time": currentEventTime,"end:time":currentEventEndTime, "end:date": currentEventEndDate]
        let timeDict = ["end":endTime, "start": startTime]

        return ["event:name":currentEventName,"event:imageURL" : currentEventImage,
                "event:description": currentEventDescription, "attend:count": currentAttendCount,
                "event:street:address": currentEventStreetAddress,"event:zip": currentEventZip,"event:price":eventPrice,
                "event:state": currentEventState, "event:city": currentEventCity, "event:promo": currentEventPromo ?? "", "event:date": dateDict, "event:category":category,"event:datetime": timeDict]
    }

    init(currentEventKey: String, dictionary: [String:Any]) {
        self.key = currentEventKey
        self.currentEventName = dictionary["event:name"] as? String ?? ""
        self.currentEventImage = dictionary["event:imageURL"] as? String ?? ""
        self.currentEventDescription = dictionary["event:description"] as? String ?? ""
        self.currentEventPromo = dictionary["event:promo"] as? String ?? ""
        self.currentAttendCount = dictionary["attend:count"] as? Int ?? 0
        self.category = dictionary["event:category"] as? String ?? ""
        self.eventPrice = dictionary["event:price"] as? String ?? ""
        //nested properties
        self.currentEventStreetAddress = dictionary["event:street:address"] as? String ?? ""
        self.currentEventCity = dictionary["event:city"] as? String ?? ""
        self.currentEventState = dictionary["event:state"] as? String ?? ""
        self.currentEventZip = dictionary["event:zip"] as? Int ?? 0
        //////
        let eventTime = dictionary["event:datetime"] as? [String: Any]
        let startInSeconds = eventTime!["start"] as? Double
        let endInSeconds = eventTime!["end"] as? Double
        self.startTime = Date(timeIntervalSince1970: startInSeconds!)
        self.endTime = Date(timeIntervalSince1970: endInSeconds!)
        ////////
        let eventDate = dictionary["event:date"] as? [String: Any]
        self.currentEventDate = eventDate?["start:date"] as? String ?? ""
        self.currentEventTime = eventDate?["start:time"] as? String ?? ""
        self.currentEventEndTime = eventDate?["end:time"] as? String ?? ""
        self.currentEventEndDate = eventDate?["end:date"] as? String ?? ""

    }




    init?(snapshot: DataSnapshot) {
        guard let dict = snapshot.value as? [String : Any],
            let currentEventName = dict["event:name"] as? String,
            let currentEventImage = dict["event:imageURL"] as? String,
            let currentEventDescription = dict["event:description"] as? String,
            let currentEventPromo = dict["event:promo"] as? String,
            let category = dict["event:category"] as? String,
            let currentEventStreetAddress = dict["event:street:address"] as? String,
            let currentEventCity = dict["event:city"] as? String,
            let currentEventState = dict["event:state"] as? String,
            let currentEventZip = dict["event:zip"] as? Int,
            let currentAttendCount = dict["attend:count"] as? Int,
            let eventPrice = dict["event:price"] as? String,
            //////
            let eventDate = dict["event:date"] as? [String: Any],
            let currentEventDate = eventDate["start:date"] as? String,
            let currentEventEndDate = eventDate["end:date"] as? String,
            let currentEventTime = eventDate["start:time"] as? String,
            let currentEventEndTime = eventDate["end:time"] as? String,
            /////
            let eventTime = dict["event:datetime"] as? [String: Any],
            let startInSeconds = eventTime["start"] as? Double,
            let endInSeconds = eventTime["end"] as? Double
            else { return nil }
        self.key = snapshot.key
        self.currentEventName = currentEventName
        self.currentEventImage = currentEventImage
        self.currentEventDescription = currentEventDescription
        self.currentEventStreetAddress = currentEventStreetAddress
        self.currentEventCity = currentEventCity
        self.currentEventState = currentEventState
        self.currentEventZip = currentEventZip
        self.currentAttendCount = currentAttendCount
        self.currentEventPromo = currentEventPromo
        self.currentEventDate = currentEventDate
        self.currentEventTime = currentEventTime
        self.currentEventEndTime = currentEventEndTime
        self.category = category
        self.eventPrice = eventPrice
        self.currentEventEndDate = currentEventEndDate
        self.endTime = Date(timeIntervalSince1970: endInSeconds)
        self.startTime = Date(timeIntervalSince1970: startInSeconds)
    }

    override func isEqual(_ object: Any?) -> Bool {
        guard let event = object as? Event else { return false }
        return self.key == event.key
    }

}

这是我数据库中的一个节点,任何人都可以帮忙吗?

   "BEVT" : {
      "attend:count" : 1,
      "event:category" : "Seize The Night",
      "event:city" : "Philadelphia",
      "event:date" : {
        "end:date" : "07/11/2018",
        "end:time" : "5:55 PM",
        "start:date" : "06/20/2018",
        "start:time" : "5:00 PM"
      },
      "event:datetime" : {
        "end" : 1531346100,
        "start" : 1529528400
      },
      "event:description" : "Travis Scott is ready to hit the road. The 24-year-old hitmaker will embark on his Birds Eye View tour, powered by Live Nation, in support of his sophomore album and Billboard 200 No. 1 Birds in the Trap Sing McKnight.\n\nScott's 25-city trek will include several festival stops, most notably Coachella and Bonnaroo. In between those performances, Scott will also touch down in major cities such as New York, Houston and Miami.",
      "event:imageURL" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/event_flyers%2FtravisScott-compressor.png?alt=media&token=c6e46da1-1395-4a37-a8fc-3ff53d5c0d4d",
      "event:name" : "birds eye view tour",
      "event:price" : "10",
      "event:promo" : "https://firebasestorage.googleapis.com/v0/b/eventful-3d558.appspot.com/o/event_promo_vid%2FBEVT%2FTravis%20Scott_%20Houston%20Birds%20Eye%20View%20Tour%20Promo.mp4?alt=media&token=6d27d76e-281e-4083-a0ff-dbe2f25703e7",
      "event:state" : "PA",
      "event:street:address" : "5748 Baltimore Pike",
      "event:zip" : 19143,
      "host_user" : [ "XwOBrK6zJXRG7wltV1QvoVXi6Jg1" ],
      "tags" : [ "concert", "travis scott", "hip hop" ]
    }

0 个答案:

没有答案