Last active
July 15, 2019 14:19
-
-
Save 0xMarK/5a6c4b93ba166e5425a1bb0fe1092dc3 to your computer and use it in GitHub Desktop.
SwiftUI (Combine) Publishers
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
class MyDefaults: BindableObject { | |
public var didChange: AnyPublisher<Bool, Never> | |
init() { | |
let defaults = UserDefaults.standard | |
didChange = Publishers.Merge( | |
defaults.publisher(for: \.userOption1), | |
defaults.publisher(for: \.userOption2) | |
) | |
.receive(on: RunLoop.main) | |
.eraseToAnyPublisher() | |
} | |
} |
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
class MyDataNotificationCenterModel: BindableObject { | |
public var didChange: NotificationCenter.default.publisher(for: .MyNotification) | |
.receive(on: RunLoop.main) | |
} | |
class MyDataKVOModel: NSObject, BindableObject { | |
public var didChange: MyObject.publisher(for: \.myKeyPath) | |
.receive(on: RunLoop.main) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment