Apple Combine 的 PassthroughSubject 未编译

Apple Combine's PassthroughSubject not Compiling

我有一个 class 可以获取用户的当前位置,我希望能够将最近获取的 CLLocationError 传递给 SwiftUI View.

下面是负责获取位置的class:

class LocationProvider: NSObject, BindableObject, CLLocationManagerDelegate {

    // MARK: - BindableObject

    var willChange = PassthroughSubject<CLLocation, Error>()

    // MARK: - CLLocationManagerDelegate

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        willChange.send(location)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        willChange.send(completion: .failure(.unknown))
    }

}

我得到以下编译错误:Type 'LocationProvider' does not conform to protocol 'BindableObject' 当我使用 Error 作为失败类型时。但是,如果我将 Error 更改为 Never,则文件编译成功。

我需要更改什么才能通过 CLLocationError

BindableObject 的 willChange 中的发布者类型必须 的错误类型为从不。如果这不是您想要的,则不能在此处使用简单的 BindableObject 的 willChange

您所追求的可能是一个可绑定对象,其发布者发布了一个结果。它可以包含位置或错误,如果您明白我的意思,您可以在管道中进一步处理它。