gpt4 book ai didi

ios - 无法打开 Promise<[CLPlacemark]>

转载 作者:行者123 更新时间:2023-11-28 05:50:36 25 4
gpt4 key购买 nike

我有一个尝试更新我的授权状态的方法。

使用 PromiseKit 6、Swift 4.2,我似乎无法让我的 Promise<[CLPlacemark]> 成为一个 CLPlacemark。

这会导致一连串的错误

Value of type 'Promise<[CLPlacemark]>' has no member 'foo'

看看我的代码:

func updateAuthStatus(status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
titleLabel.isHidden = true
bigIconView.isHidden = true
descriptionLabel.isHidden = true
grantBtn.isHidden = true
titleLabel1.isHidden = false
loaderView.isHidden = false
loaderView.resumeSpinning()
CLLocationManager.requestLocation().compactMapValues { location in
return self.coder.reverseGeocode(location: location)
}.compactMapValues { placemark -> Void in
if placemark.ISOcountryCode == "CA" {
self.seal.fulfill(())
} else if placemark.ISOcountryCode == "US" {
if self.allowedStates.contains(placemark.administrativeArea ?? "") {
self.seal.fulfill(())
} else {
self.seal.reject(LocationError.InvalidState(placemark.administrativeArea ?? ""))
}
} else {
self.seal.reject(LocationError.InvalidCountry(placemark.ISOcountryCode ?? ""))
}
}
}
else if status == .denied || status == .restricted {
if denied == false {
self.seal.reject(LocationError.Unknown)
}
denied = true
}
}

最佳答案

正如您所提到的,此promise 返回一个CLPlacemark 对象数组,因此您需要从 中获取特定的placemark array 然后使用它。这是访问 array 中的 first placemark 的示例。

.compactMapValues { placemarks -> Void in
guard let placemark = placemarks.first else { return }

if placemark.ISOcountryCode == "CA" {
self.seal.fulfill(())
} else if placemark.ISOcountryCode == "US" {
if self.allowedStates.contains(placemark.administrativeArea ?? "") {
self.seal.fulfill(())
} else {
self.seal.reject(LocationError.InvalidState(placemark.administrativeArea ?? ""))
}
} else {
self.seal.reject(LocationError.InvalidCountry(placemark.ISOcountryCode ?? ""))
}
}

编辑

要处理美国和加拿大,可以提取该国家的相关地标,如下所示,

.compactMapValues { placemarks -> Void in
if let ca = placemarks.filter{ $0.ISOcountryCode == "CA"}.first {
self.seal.fulfill(())
} else if let usa = placemarks.filter{ $0.ISOcountryCode == "US"}.first {
if self.allowedStates.contains(usa.administrativeArea ?? "") {
self.seal.fulfill(())
} else {
let error = LocationError.InvalidState(usa.administrativeArea ?? "")
self.seal.reject(error)
}
} else {
let error = LocationError.InvalidCountry(placemarks.first?.ISOcountryCode ?? "")
self.seal.reject(error)
}
}

关于ios - 无法打开 Promise<[CLPlacemark]>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53129989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com