This file contains hidden or 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
| _ = dataProvider | |
| .decode(type: DummyDecodable.self, decoder: JSONDecoder()) | |
| .map({ (decodable) -> String in | |
| return "The user is \(decodable.userName) and his id is \(decodable.userId)" | |
| }) | |
| .sink(receiveCompletion: { completion in | |
| print(completion) | |
| }, receiveValue: { response in | |
| print(response) | |
| }) |
This file contains hidden or 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
| // mapping | |
| _ = [1,2,3,5,8,12] | |
| .publisher | |
| .map { $0%2 } | |
| .sink(receiveValue: { (value) in | |
| print(value) | |
| }) | |
| Output: 1 0 1 1 0 0 |
This file contains hidden or 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 | |
| .handleEvents( | |
| receiveSubscription: { (subscription) in | |
| print("Received a subscription: \(subscription)") | |
| }, receiveOutput: { (output) in | |
| print("Received an output: \(output)") | |
| }, receiveCompletion: { (completion) in | |
| switch completion { | |
| case .finished: | |
| print("Finished with success") |
This file contains hidden or 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 = CurrentValueSubject<String, Never>("This text should not be sent") | |
| subject.send("😉") | |
| let subscriber1 = subject.sink { (value) in | |
| print("First subscriber received value: \(value)") | |
| } | |
| subject.send("🐶") | |
| let subscriber2 = subject.sink { (value) in |
This file contains hidden or 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 = CurrentValueSubject<String, Never>("This text should not be received") | |
| subject.send("😉") | |
| subject.sink { (value) in | |
| print("Received value: \(value)") | |
| } | |
| subject.send("🐶”) | |
| Output: | |
| Received value: 😉 | |
| Received value: 🐶 |
This file contains hidden or 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 = PassthroughSubject<String, Never>() | |
| subject.send("😉") | |
| subject.sink { (value) in | |
| print("Received value: \(value)") | |
| } | |
| subject.send("🐶”) | |
| Output: |
This file contains hidden or 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 sequence = Publishers.Sequence<[String], Never>(sequence: ["Hello", "World”]) | |
| sequence | |
| .sink(receiveCompletion: { (completion) in | |
| switch completion { | |
| case .finished: | |
| print("Finished") | |
| case .failure(let error): | |
| print("Finished with error \(error)") | |
| } |
This file contains hidden or 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 welcome = "Welcome”.publisher | |
| welcome.sink { (value) in | |
| print(value) | |
| } | |
| Output: W e l c o m e |
This file contains hidden or 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 pets = ["🐶", "🐱", "🐭", "🐹"].publisher // pets type is Publishers.Sequence<Array<String>, Never> | |
| pets.sink { (value) in | |
| print(value) | |
| } |
This file contains hidden or 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 just = Just("First publisher!") | |
| let sequence = Publishers.Sequence<[String], Never>(sequence: ["Hello", "World"]) | |
| just.sink { (value) in | |
| print(value) | |
| } | |
| sequence.sink { (value) in | |
| print(value) | |
| } |