无法打开 Promise<[CLPlacemark]>
Unable to unwrap Promise<[CLPlacemark]>
我有一个尝试更新我的授权状态的方法。
使用 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
对象数组,因此您需要从 array
中获取特定的 placemark
然后使用它。这是通过访问 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{ [=11=].ISOcountryCode == "CA"}.first {
self.seal.fulfill(())
} else if let usa = placemarks.filter{ [=11=].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)
}
}
我有一个尝试更新我的授权状态的方法。
使用 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
对象数组,因此您需要从 array
中获取特定的 placemark
然后使用它。这是通过访问 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{ [=11=].ISOcountryCode == "CA"}.first {
self.seal.fulfill(())
} else if let usa = placemarks.filter{ [=11=].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)
}
}