让我们不断发行

时间:2018-01-24 04:12:26

标签: ios swift

我有这个功能,它从特定用户参加的firebase中获取事件。一切似乎都在努力,直到我尝试相对于事件快照更改元素。

我收到错误

  

无法分配属性:'event'是'let'常量

    //will show the vents that a user is attending
    static func Events(for user: User, completion: @escaping ([Event]) -> Void)
    {
        var currentEvents = [Event]()

        //Getting firebase root directory
        let ref = Database.database().reference().child("users").child(user.uid).child("Attending")


        ref.observe(.value, with: { (snapshot) in
         //   print(snapshot)

//            guard snapshot.children.allObjects is [DataSnapshot] else {
//                return completion([])
//            }

            guard let eventDictionary = snapshot.value as? [String: Any] else {
                return completion([])
            }

          //  print(snapshot)
            let dispatchGroup = DispatchGroup()

            eventDictionary.forEach({ (key,value) in
             //   print(key)
             //   print(value)
                EventService.show(forEventKey: key , completion: { (event) in
                    dispatchGroup.enter()
                    AttendService.isEventAttended(event, byCurrentUserWithCompletion: { (isAttended) in
//error happens here
                        event?.isAttending = isAttended
                        dispatchGroup.leave()

                    })
                     currentEvents.append(.init(currentEventKey: key , dictionary: (event?.eventDictionary)!))
                    completion(currentEvents)
                })
            })


        })
    }

这对我来说真的很困惑,因为我无法看到我把它宣布为一个常数。

这是我用来收集活动信息的方法的代码

static func show(forEventKey eventKey: String, completion: @escaping (Event?) -> Void) {
        // print(eventKey)
        let ref = Database.database().reference().child("events").child(eventKey)
       //  print(eventKey)
        //pull everything
        ref.observeSingleEvent(of: .value, andPreviousSiblingKeyWith: { (snapshot,eventKey) in
        //    print(snapshot.value ?? "")
            guard let event = Event(snapshot: snapshot) else {
                return completion(nil)
            }

           completion(event)
        })
    }

以下是我的事件对象模型

import Foundation
import FirebaseDatabase.FIRDataSnapshot


struct Event: Keyed {
    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 currentEventTime: String?
    let currentEventEndTime: String?
    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]

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

    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 ?? ""
        //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 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 ?? ""

    }

    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 eventDate = dict["event:date"] as? [String: Any],
            let currentEventDate = eventDate["start:date"] as? String,
            let currentEventTime = eventDate["start:time"] as? String,
            let currentEventEndTime = eventDate["end:time"] as? String
            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
    }





}

1 个答案:

答案 0 :(得分:0)

Eventstruct,因此其实例按值传递。也就是说,来电completion(event)会将event的副本传递给completion

由于completion并未将Event视为inout,因此将其视为let,而不是var,因此您无法修改它。

您可以将completion的类型更改为@escaping (inout Event?) -> Void。然后,Event将被视为var,对其的更改将被有效地复制回调用方。

但是,这不会对您的情况产生影响,因为您正在为Event调用创建一个全新的completion实例(通过说guard let event = Event(snapshot: snapshot) })。因此,即使您让completion修改它收到的Event,您也可以将其丢弃。如果您希望更改completion产生任何影响,还需要在isAttending返回后以某种方式将其写回数据库。