Cannot use mutating member on immutable value error when modifying a struct

Viewed 11104

I have this simple struct.

struct Section {
    let store: Store
    var offers: [Offer]
}

In the VC, I have declared an array of these Sections at the top like so, fileprivate var sections: [Section] = []. And I add some Section objects to in the viewDidLoad().

Later, I need to delete some Offer objects from the offers array inside some Sections.

I iterate through the sections array to find the Section that contains the Offer that needs to be deleted.

for section in sections {
    if let i = section.offers.index(where: { $0.id == offer.id }) {
        section.offers.remove(at: i) // Cannot use mutating member on immutable value: 'section' is a 'let' constant
    }
}

But when I try to delete that particular Offer from the offers array, I get the error Cannot use mutating member on immutable value: 'section' is a 'let' constant.

How do I resolve this?

4 Answers
Related