Skip to content

Commit

Permalink
Update playground for Combine
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Jul 3, 2019
1 parent 5434488 commit 3ce36b4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ enum FormError: Error { }
let usernamePublisher = PassthroughSubject<String, FormError>()
let passwordPublisher = PassthroughSubject<String, FormError>()

let validatedCredentials = Publishers.CombineLatest(usernamePublisher, passwordPublisher, transform: { (username, password) -> (String, String) in
let validatedCredentials = Publishers.CombineLatest(usernamePublisher, passwordPublisher)
.map { (username, password) -> (String, String) in
return (username, password)
})
}
.map({ (username, password) -> Bool in
!username.isEmpty && !password.isEmpty && password.count > 12
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NotificationCenter.default.publisher(for: .NSSystemClockDidChange)
##### KeyPath binding to NSObject instances
*/
let ageLabel = UILabel()
Publishers.Just(28)
Just(28)
.map { "Age is \($0)" }
.assign(to: \.text, on: ageLabel)
//: [Next](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func fetchUser(for userId: Int, completion: (_ result: Result<User, FetchError>)
let fetchUserPublisher = PassthroughSubject<Int, FetchError>()

fetchUserPublisher
.flatMap { userId -> Publishers.Future<User, FetchError> in
return Publishers.Future { promise in
.flatMap { userId -> Future<User, FetchError> in
return Future { promise in
fetchUser(for: userId) { (result) in
switch result {
case .success(let user):
Expand All @@ -41,9 +41,9 @@ fetchUserPublisher
}
}
.map { user in user.name }
.catch({ (error) -> Publishers.Just<String> in
.catch({ (error) -> Just<String> in
print("Error occurred: \(error)")
return Publishers.Just("Not found")
return Just("Not found")
})
.sink { result in
print("User is \(result)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Combine
- ..a subscriber subscribes to receive all those updates

*/
let publisher = Publishers.Just(28)
let publisher = Just(28)

// This creates a `Subscriber` on the `Just a 28` Publisher
publisher
Expand Down

0 comments on commit 3ce36b4

Please sign in to comment.