代码之家  ›  专栏  ›  技术社区  ›  Abhishek Thapliyal

RxSwift:BehaviorRelay代替变量用法

  •  0
  • Abhishek Thapliyal  · 技术社区  · 7 年前

    我是RxSwift的新手,读过一些关于主题的书,我试过了 Variable 主题。依次在控制台发出警告

    ℹ️ [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 原来是这样的 get set 财产状

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

    收到警告后我把它改成了 BehaviorRelay

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

    所以我犯了个错误 值仅为get属性 .

    我在google上搜索了很多,但是找不到关于数组操作的合适解释。

    我只有密码 self.searchItems.accept(items)

    我需要如何所有4个操作将执行时使用 行为规则 ?

    2 回复  |  直到 7 年前
        1
  •  6
  •   maxwell    7 年前

    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)
    
        2
  •  5
  •   Daniel T.    7 年前

    使用 accept .

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