0

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] = []

2 Answers 2

2

A very common mistake.

Indexes are zero-based. Imagine your array has one element. The range 0...array.count has the indexes 0 and 1 but there is no element at index 1...


You have to write either

for index in 0..<self.historyBooking.count

or

for index in 0...self.historyBooking.count - 1

The first syntax is preferable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the detailed answer. I hope now I will no longer get this error.
0

To avoid Index out of bound you can use for...in loop

for obj in 0...self.historyBooking.count {

  self.hallArray.append(obj.hall)

  print("hall id \(self.hallArray)")

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.