我有一个获取用户当前位置的类,我希望能够将最近获取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
用作失败类型时。但是,如果我更改Error
为Never
,则文件编译成功。
我需要更改什么才能通过 aCLLocation
或 an Error
?