转义完成处理程序永远不会发生(Google 地方 API)
Escaping completion handler never happens (Google Places API)
我正在构建一个使用 Google 地点 API 的应用。我目前有一个按钮,点击该按钮可获取当前 GPS 位置的地址。代码在我的视图控制器中:
var placesClient: GMSPlacesClient?
@IBAction func buttonTapped(_ sender: AnyObject) {
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
self.addressLabel.text = place?.formattedAddress!.components(separatedBy: ", ").joined(separator: "\n")
}
})
print("Out of the brackets...")
}
这样完成后,函数完成并打印 "Out of the brackets..."。
但是,当我尝试将此代码移出视图控制器并移入自定义 class 并从视图控制器调用它时,如下所示,"placesClient?.currentPlace(callback" 块中的所有内容都会运行(并且检索正确的地址),但 "Out of the brackets..." 永远不会被打印出来,它也永远不会 returns 值:
class LocationAPIService {
var placesClient: GMSPlacesClient? = GMSPlacesClient.shared()
func getCurrentLocation() -> GMSPlace? {
var thisPlace: GMSPlace?
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
thisPlace = place
}
})
print("Out of the brackets...")
return thisPlace
}
}
有人知道为什么会这样吗?
已修复。这是我在 LocationAPIService class:
中用于该方法的代码
func setCurrentLocationPlace(completion: @escaping (_ result: Bool)->()) {
var placeFindComplete: Bool = false
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
completion(true)
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
self.currentPlace = place
placeFindComplete = true
completion(true)
}
})
if (placeFindComplete == false) {
completion(false)
}
}
这是我从视图控制器中调用它的方式:
locationAPIService?.setCurrentLocationPlace() { (locationFound) -> () in
if (locationFound == true) {
//Run code here that triggers once setCurrentLocationPlace() complete.
}
我正在构建一个使用 Google 地点 API 的应用。我目前有一个按钮,点击该按钮可获取当前 GPS 位置的地址。代码在我的视图控制器中:
var placesClient: GMSPlacesClient?
@IBAction func buttonTapped(_ sender: AnyObject) {
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
self.addressLabel.text = place?.formattedAddress!.components(separatedBy: ", ").joined(separator: "\n")
}
})
print("Out of the brackets...")
}
这样完成后,函数完成并打印 "Out of the brackets..."。
但是,当我尝试将此代码移出视图控制器并移入自定义 class 并从视图控制器调用它时,如下所示,"placesClient?.currentPlace(callback" 块中的所有内容都会运行(并且检索正确的地址),但 "Out of the brackets..." 永远不会被打印出来,它也永远不会 returns 值:
class LocationAPIService {
var placesClient: GMSPlacesClient? = GMSPlacesClient.shared()
func getCurrentLocation() -> GMSPlace? {
var thisPlace: GMSPlace?
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
thisPlace = place
}
})
print("Out of the brackets...")
return thisPlace
}
}
有人知道为什么会这样吗?
已修复。这是我在 LocationAPIService class:
中用于该方法的代码func setCurrentLocationPlace(completion: @escaping (_ result: Bool)->()) {
var placeFindComplete: Bool = false
placesClient?.currentPlace(callback: { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
completion(true)
return
}
if let placeLikelihoods = placeLikelihoods {
let place = placeLikelihoods.likelihoods.first?.place
self.currentPlace = place
placeFindComplete = true
completion(true)
}
})
if (placeFindComplete == false) {
completion(false)
}
}
这是我从视图控制器中调用它的方式:
locationAPIService?.setCurrentLocationPlace() { (locationFound) -> () in
if (locationFound == true) {
//Run code here that triggers once setCurrentLocationPlace() complete.
}