gpt4 book ai didi

ios - 让问题不断

转载 作者:行者123 更新时间:2023-11-30 11:50:10 26 4
gpt4 key购买 nike

我有一个函数,可以从特定用户正在参加的 Firebase 中获取事件。一切似乎都在正常进行,直到我尝试更改相对于事件快照的元素。

我收到错误

Cannot assign to property: 'event' is a 'let' constant

    //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)
})
})


})
}

这对我来说真的很困惑,因为我看不到在哪里将它声明为 let 常量。

这是我用来收集事件信息的方法的代码

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
}





}

最佳答案

Event 是一个struct,因此它的实例是按值传递的。也就是说,调用 completion(event)event 的副本传递给 completion

由于 completion 不将 Event 作为 inout,因此它被视为 let,不是 var,因此您无法修改它。

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

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

关于ios - 让问题不断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com