有没有办法不触发变量<T>的初始值?
Is there a way to not trigger the init value of the Variable<T>?
见下面的代码。
class ViewController4: UIViewController {
var disposeBag = DisposeBag()
let v = Variable(0)
override func viewDidLoad() {
super.viewDidLoad()
v.asObservable()
.subscribe(onNext: { print([=11=]) })
.disposed(by: disposeBag)
v.value = 1
}
}
当它 运行 时,它会打印
0
1
但是,我不希望它在 0
上 运行,或者说 0
只是用于启动 v
的值。我可以这样做吗?还是我在使用的时间点必须推迟代码?
您可以使用运算符 .skip
来抑制发出的第 N 个元素。因此,在您的情况下 skip(1)
将抑制初始值。
v.asObservable().skip(1)
.subscribe(onNext: { print([=10=]) })
.disposed(by: disposeBag)
v.value = 1
//Output : 1
v.value = 2
v.value = 3
//Output : 1 2 3
见下面的代码。
class ViewController4: UIViewController {
var disposeBag = DisposeBag()
let v = Variable(0)
override func viewDidLoad() {
super.viewDidLoad()
v.asObservable()
.subscribe(onNext: { print([=11=]) })
.disposed(by: disposeBag)
v.value = 1
}
}
当它 运行 时,它会打印
0
1
但是,我不希望它在 0
上 运行,或者说 0
只是用于启动 v
的值。我可以这样做吗?还是我在使用的时间点必须推迟代码?
您可以使用运算符 .skip
来抑制发出的第 N 个元素。因此,在您的情况下 skip(1)
将抑制初始值。
v.asObservable().skip(1)
.subscribe(onNext: { print([=10=]) })
.disposed(by: disposeBag)
v.value = 1
//Output : 1
v.value = 2
v.value = 3
//Output : 1 2 3