Skip to content

Instantly share code, notes, and snippets.

@0xMarK
Last active July 15, 2019 14:19
Show Gist options
  • Save 0xMarK/5a6c4b93ba166e5425a1bb0fe1092dc3 to your computer and use it in GitHub Desktop.
Save 0xMarK/5a6c4b93ba166e5425a1bb0fe1092dc3 to your computer and use it in GitHub Desktop.
SwiftUI (Combine) Publishers
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()
}
}
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