0

我有一个获取用户当前位置的类,我希望能够将最近获取CLLocation的或 anError传递给SwiftUI View.

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

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用作失败类型时。但是,如果我更改ErrorNever,则文件编译成功。

我需要更改什么才能通过 aCLLocation或 an Error

4

1 回答 1

0

BindableObject 中的发布者类型willChange 必须具有 Never 错误类型。如果这不是你想要的,你不能willChange在这里使用简单的 BindableObject。

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

于 2019-07-22T18:49:03.213 回答