RxSwift:BehaviorRelay代替变量用法 [英] RxSwift : BehaviorRelay in place of Variable usage

查看:330
本文介绍了RxSwift:BehaviorRelay代替变量用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是RxSwift的新手,正在阅读有关主题的信息,我尝试了 Variable 主题。依次在控制台中发出警告

I'm new to RxSwift and reading about subjects, I tried Variable Subject. Which in turns giving Warning in console

ℹ️ [DEPRECATED] `Variable` is planned for future deprecation. Please consider `BehaviorRelay` as a replacement. Read more at: https://git.io/vNqvx

我早些时候曾声明像这样的变量

var searchItems = Variable<[MyClass]>([])

所以我已经通过名为 value 就像获得设置属性一样

So i have done basic array operations from it's property called value as it was get set property like

 1. self.searchItems.value.removeAll()
 2. self.searchItems.value.append(items)
 3. self.searchItems.value = items

现在收到警告后,我将其更改为 BehaviorRelay

Now After getting warning i changed it to BehaviorRelay like

var searchItems = BehaviorRelay<[MyClass]>(value: [])

所以我得到了一个错误,即值仅是获得财产

So I got error that value is get property only.

我在Google上搜索了很多但无法获得

I googled alot but can't get suitable explanations for Array operations.

我只有一个代码 self.searchItems.accept(items)别 知道它到底是怎么添加新项目或追加的。

I only got a code self.searchItems.accept(items) which i really don't know what it exactly do add fresh items or append.

我需要在使用 BehaviorRelay ?

推荐答案

1)删除所有

var array = self.searchItems.value
array.removeAll()
self.searchItems.accept(array)

2)附加项目

self.searchItems.value.accept(searchItems + [items])

3)值= ...

self.searchItems.value.accept(items)

这篇关于RxSwift:BehaviorRelay代替变量用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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