Created
December 1, 2018 15:00
-
-
Save YutoMizutani/2aa20dbad3d6ace238c5a9270fba91da to your computer and use it in GitHub Desktop.
RxSwiftで引数付きの関数を追加 (extension) する場合は気をつけるという話 ref: https://qiita.com/YutoMizutani/items/de082fde1c7c3b111195
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Observable where E == Int { | |
func add(_ value: Int) -> Observable<Int> { | |
return map { $0 + value } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let subject = PublishSubject<Int>() | |
var value = 10 | |
_ = subject | |
.add(value) | |
.subscribe(onNext: { print($0) }) | |
value = 5 | |
subject.onNext(0) // 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Observable where E == Int { | |
func add(_ value: @escaping () -> Int) -> Observable<Int> { | |
return map { $0 + value() } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = subject | |
.add({ value }) | |
.subscribe(onNext: { print($0) }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Observable where E == Int { | |
func add(_ value: @escaping @autoclosure () -> Int) -> Observable<Int> { | |
return map { $0 + value() } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let subject = PublishSubject<Int>() | |
var value = 10 | |
_ = subject | |
.add(value) | |
.subscribe(onNext: { print($0) }) | |
value = 5 | |
subject.onNext(0) // 5 | |
value = 20 | |
subject.onNext(0) // 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment