在 RxSwift 中修改 behaviorRelay 数组内的属性 [英] Modify property inside of a behaviourRelay array in RxSwift

查看:102
本文介绍了在 RxSwift 中修改 behaviorRelay 数组内的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 RxSwift 定义的数组

I have an array defined using RxSwift as

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

CalendarControlDayModel 结构如下.

CalendarControlDayModel is a structure as below.

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

一旦 calendarNDays 在某个时间点更新了元素,我想修改数组中第 i 个元素的 name 属性.

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.

喜欢 self.calendarNDays.value[i].name = "Nancy".但是,我收到编译错误无法分配给属性:'value' 是一个只能获取的属性".

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?

推荐答案

正如编译器所建议的,BehaviorRelay 中的 value 是只读属性.

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

因此,为了对数组进行更改,您首先需要复制它并使用 accept 方法来反映更改.

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

类似

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

这篇关于在 RxSwift 中修改 behaviorRelay 数组内的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆