I have class in which have variable hall and other variables. I need to need pull out and add variable hall in new array of strings.
All variables are now stored in a variable with type BookingHall.
How do I get and add a variable hall to a new variable?
For everything I use firestore.
My class implementation:
protocol BookingDocumentSerializable {
init?(dictionary: [String:Any])
}
struct BookingHall {
var contactInfo: [String: Any] = [:]
................
var hall: String = ""
var dictionary: [String: Any] {
return [
"contact_info": contactInfo,
................
"hall": hall
]
}
}
extension BookingHall: BookingDocumentSerializable {
init?(dictionary: [String: Any]) {
let contactInfo = dictionary["contact_info"] as? [String: Any] ?? [:]
................
let hall = dictionary["hall"] as? String ?? ""
self.init(contactInfo: contactInfo,
................
hall: hall)
}
}
Here i get all variable in class:
var hallArray: [String] = []
private var historyBooking: [BookingHall] = []
fileprivate func observeQuery() {
guard let query = query else { return }
listener = query.addSnapshotListener { [unowned self] (snapshot, error) in
if let snapshot = snapshot {
let bookingModel = snapshot.documents.map { (document) -> BookingHall in
if let newHistoryBooking = BookingHall(dictionary: document.data()) {
return newHistoryBooking
} else {
fatalError("Ошибка загрузки!")
}
}
self.historyBooking = bookingModel
self.document = snapshot.documents
for index in 0...self.historyBooking.count {
self.hallArray.append(self.historyBooking[index].hall)
print("hall id \(self.hallArray)")
}
self.tableView.reloadData()
}
}
}
In here I get error index out of range, but in console I can see hall id.
self.hallArray.append(self.historyBooking[index].hall)
How do I correctly add hall to a new array of strings in...
var hallArray: [String] = []