相关疑难解决方法(0)

Swift Joint:这些多播函数的用途是什么以及如何使用它们?

在努力解决一些联合收割机问题时,我遇到了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)

swift ios13 combine macos-catalina

4
推荐指数
1
解决办法
3343
查看次数

Swift Joint 的CombineLatest 不会响应其发布者之一的更新而触发

我正在结合两个发布者来确定地图视图的中心坐标应该是什么。这两家出版商是:

  1. 用户的初始位置由 a 确定CLLocationManager(开始发送位置更新后报告的第一个位置CLLocationManager)。
  2. 如果点击“当前位置的中心地图”按钮,则显示用户的当前位置。

在代码中:

    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)

swift combinelatest combine

4
推荐指数
1
解决办法
1734
查看次数

标签 统计

combine ×2

swift ×2

combinelatest ×1

ios13 ×1

macos-catalina ×1