Kone
Kone

Reputation: 33

Modify property inside of a behaviourRelay array in RxSwift

I have an array defined using RxSwift as

public var calendarNDays = BehaviorRelay<[CalendarControlDayModel]>(value: [])

CalendarControlDayModel is a structure as below.

struct CalendarControlDayModel {
        var date: String = ""
        var day: Int = 0
        var name: String = "" 
}

Once the calendarNDays is updated with elements at some point of time I want to modify the name property of i-th element in the array.

Like self.calendarNDays.value[i].name = "Nancy". However, I get the compilation error "Cannot assign to property: 'value' is a get-only property".

What is the way to modify a particular property of an element in a behaviour relay array?

Upvotes: 0

Views: 1642

Answers (1)

Peter Pajchl
Peter Pajchl

Reputation: 2709

As the compiler suggests the value in BehaviorRelay is a read-only property.

Therefore in order to make changes to the array you first need to copy it and use the accept method to reflect the changes.

Similar to

var update = calendarNDays.value
update[i].name = “Nancy”
calendarNDays.accept(update)

Upvotes: 4

Related Questions