在努力解决一些联合收割机问题时,我遇到了https://developer.apple.com/documentation/combine/publisher中的“使用多个订阅者”部分:
func multicast<S>(() -> S) -> Publishers.Multicast<Self, S>
func multicast<S>(subject: S) -> Publishers.Multicast<Self, S>
Run Code Online (Sandbox Code Playgroud)
然而,当我试图确认我的假设时,在发送给多个订阅者时需要多播,我发现在尝试这个游乐场代码时这是不必要的(修改自https://github.com/AvdLee/CombineSwiftPlayground/blob/ ) master/Combine.playground/Pages/Combining%20Publishers.xcplaygroundpage/Contents.swift)(在 Xcode 版本 11.0 beta 3 (11M362v) 中的 10.14.5 上运行):
enum FormError: Error { }
let usernamePublisher = PassthroughSubject<String, FormError>()
let passwordPublisher = PassthroughSubject<String, FormError>()
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
}
.eraseToAnyPublisher()
let firstSubscriber = …Run Code Online (Sandbox Code Playgroud) 我正在结合两个发布者来确定地图视图的中心坐标应该是什么。这两家出版商是:
CLLocationManager(开始发送位置更新后报告的第一个位置CLLocationManager)。在代码中:
class LocationManager: NSObject, ObservableObject {
// The first location reported by the CLLocationManager.
@Published var initialUserCoordinate: CLLocationCoordinate2D?
// The latest location reported by the CLLocationManager.
@Published var currentUserCoordinate: CLLocationCoordinate2D?
// What the current map view center should be.
@Published var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977) // Default coordinate.
// A subject whose `send(_:)` method is being called elsewhere every time the user presses a button to center the …Run Code Online (Sandbox Code Playgroud)