As of RxSwift4, Variable
is moved to Deprecated.swift
marking the possible deprecation of Variable
in future. An alternate proposed to
Building on Dalton's answer, here is a handy extension:
extension BehaviorRelay where Element: RangeReplaceableCollection {
func acceptAppending(_ element: Element.Element) {
accept(value + [element])
}
}
I wrote this extension for replacing Variable
s with BehaviorRelay
s. You can add whatever method you need based on this pattern to migrate easily.
public extension BehaviorRelay where Element: RangeReplaceableCollection {
public func insert(_ subElement: Element.Element, at index: Element.Index) {
var newValue = value
newValue.insert(subElement, at: index)
accept(newValue)
}
public func insert(contentsOf newSubelements: Element, at index: Element.Index) {
var newValue = value
newValue.insert(contentsOf: newSubelements, at: index)
accept(newValue)
}
public func remove(at index: Element.Index) {
var newValue = value
newValue.remove(at: index)
accept(newValue)
}
}
Instead of Variable.value.funcName
, now you write BehaviorRelay.funcName
.
The idea to use where Element: RangeReplaceableCollection
clause comes from retendo's answer
Also note that the index
is of type Element.Index
, not Int
or whatever else.