Ios 我如何从这次关闭中获取数据?
我是swift中闭包的新手,想知道如何从闭包中获取注释。我知道,由于它是异步的,外部阵列不会在正确的时间被填满 我应该使用类似于完成处理程序的东西吗?我该怎么做?这方面的最佳做法是什么?从Ios 我如何从这次关闭中获取数据?,ios,swift,closures,google-places-api,Ios,Swift,Closures,Google Places Api,我是swift中闭包的新手,想知道如何从闭包中获取注释。我知道,由于它是异步的,外部阵列不会在正确的时间被填满 我应该使用类似于完成处理程序的东西吗?我该怎么做?这方面的最佳做法是什么?从didupdateLocation调用具有回调的函数 fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int) -&g
didupdateLocation
调用具有回调的函数
fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int) -> Array<GMAnnotation>
{
var annotations: [GMAnnotation] = []
placesClient.currentPlace(callback: { (placeLikelihoods, error) -> Void in
if let error = error {
print("Current Place error: \(error.localizedDescription)")
return
}
if let likelihoodList = placeLikelihoods {
for likelihood in likelihoodList.likelihoods {
let annotation = GMAnnotation()
let place = likelihood.place
annotation.location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
annotations.append(annotation)
}
}
})
return annotations; ====> EMPTY
fileprivate func getGooglePoissforCurrentLocation(中心纬度:双精度,中心经度:双精度,增量:双精度,计数:Int)->数组
{
变量注释:[GMAnnotation]=[]
当前位置(回调:{(placeLikelihoods,error)->中的Void
如果let error=error{
打印(“当前位置错误:\(error.localizedDescription)”)
返回
}
如果let likelihoodList=placeLikelihoods{
对于似然度列表中的似然度。似然度{
let annotation=GMAnnotation()
让地点
annotation.location=CLLocation(纬度:place.coordinate.lation,经度:place.coordinate.longitude)
annotations.append(注释)
}
}
})
返回注释;=>空
嗯,currentPlace
调用是异步的,因此您不能立即返回getGooglePoissforCurrentLocation
函数中的任何内容
根据您的需要,您可以这样做:
- 将注释存储在ViewController的私有实例变量中
- 调用一些UI刷新代码(如
或ViewController中的其他东西)来更新UI。请注意,这样的调用必须在主线程中完成(可以使用tableView.reloadData
来完成)DispatchQueue.main.async
currentPlace
调用是异步的,因此您不能立即返回getGooglePoissforCurrentLocation
函数中的任何内容
根据您的需要,您可以这样做:
- 将注释存储在ViewController的私有实例变量中
- 调用一些UI刷新代码(如
或ViewController中的其他东西)来更新UI。请注意,这样的调用必须在主线程中完成(可以使用tableView.reloadData
来完成)DispatchQueue.main.async
@escaping
闭包。请记住,异步函数会改变应用程序的工作方式—例如,如果您在表视图中显示此数据,则需要调用tableView.reloadData()
(或其同级方法之一)在数据可用后实际更新表。以下是带有闭包的函数:
fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int, closure: @escaping (Array<GMAnnotation>) -> Void) {
var annotations: [GMAnnotation] = []
placesClient.currentPlace(callback: { (placeLikelihoods, error) -> Void in
if let likelihoodList = placeLikelihoods {
for likelihood in likelihoodList.likelihoods {
let annotation = GMAnnotation()
let place = likelihood.place
annotation.location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
annotations.append(annotation)
}
closure(annotations)
}
})
}
作为旁注,此示例并不特别安全-没有错误检查,返回类型也不是可选的-如果API调用失败,它可能会崩溃。下面是同一函数的更健壮版本,它处理错误和正确的返回类型:
enum PlacesResult {
case success(Array<GMAnnotation>)
case error(Error)
}
fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int, closure: @escaping (PlacesResult) -> Void) {
var annotations: [GMAnnotation] = []
placesClient.currentPlace(callback: { (placeLikelihoods, error) -> Void in
if let error = error {
print("Current Place error: \(error.localizedDescription)")
closure(PlacesResult.error(error))
}
if let likelihoodList = placeLikelihoods {
for likelihood in likelihoodList.likelihoods {
let annotation = GMAnnotation()
let place = likelihood.place
annotation.location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
annotations.append(annotation)
}
closure(PlacesResult.success(annotations))
} else {
closure(PlacesResult.error(Error()))
// something else went wrong but we still want this to call the closure.
}
})
}
枚举位置结果{
案例成功(阵列)
案例错误(错误)
}
fileprivate func GetGooglePoissforCurrentLocation(中心纬度:双精度,中心经度:双精度,增量:双精度,计数:Int,闭包:@escaping(PlacesResult)->Void){
变量注释:[GMAnnotation]=[]
当前位置(回调:{(placeLikelihoods,error)->中的Void
如果let error=error{
打印(“当前位置错误:\(error.localizedDescription)”)
闭包(PlacesResult.error(错误))
}
如果let likelihoodList=placeLikelihoods{
对于似然度列表中的似然度。似然度{
let annotation=GMAnnotation()
让地点
annotation.location=CLLocation(纬度:place.coordinate.lation,经度:place.coordinate.longitude)
annotations.append(注释)
}
结束(PlacesResult.success(注释))
}否则{
闭包(PlacesResult.error(error()))
//其他一些事情出了问题,但我们仍然希望这叫做结束。
}
})
}
是的,您需要在函数中添加一个@escaping
闭包。请记住,异步函数会改变应用程序的工作方式—例如,如果您在表视图中显示此数据,则需要调用tableView.reloadData()
(或其同级方法之一)在数据可用后实际更新表。以下是带有闭包的函数:
fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int, closure: @escaping (Array<GMAnnotation>) -> Void) {
var annotations: [GMAnnotation] = []
placesClient.currentPlace(callback: { (placeLikelihoods, error) -> Void in
if let likelihoodList = placeLikelihoods {
for likelihood in likelihoodList.likelihoods {
let annotation = GMAnnotation()
let place = likelihood.place
annotation.location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
annotations.append(annotation)
}
closure(annotations)
}
})
}
作为旁注,此示例并不特别安全-没有错误检查,返回类型也不是可选的-如果API调用失败,它可能会崩溃。下面是同一函数的更健壮版本,它处理错误和正确的返回类型:
enum PlacesResult {
case success(Array<GMAnnotation>)
case error(Error)
}
fileprivate func getGooglePoisForCurrentLocation(centerLatitude: Double, centerLongitude: Double, delta: Double, count: Int, closure: @escaping (PlacesResult) -> Void) {
var annotations: [GMAnnotation] = []
placesClient.currentPlace(callback: { (placeLikelihoods, error) -> Void in
if let error = error {
print("Current Place error: \(error.localizedDescription)")
closure(PlacesResult.error(error))
}
if let likelihoodList = placeLikelihoods {
for likelihood in likelihoodList.likelihoods {
let annotation = GMAnnotation()
let place = likelihood.place
annotation.location = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
annotations.append(annotation)
}
closure(PlacesResult.success(annotations))
} else {
closure(PlacesResult.error(Error()))
// something else went wrong but we still want this to call the closure.
}
})
}
枚举位置结果{
案例成功(阵列)
案例错误(错误)
}
fileprivate func GetGooglePoissforCurrentLocation(中心纬度:双精度,中心经度:双精度,增量:双精度,计数:Int,闭包:@escaping(PlacesResult)->Void){
变量注释:[GMAnnotation]=[]
当前位置(回调:{(placeLikelihoods,error)->中的Void
如果let error=error{
打印(“当前位置错误:\(error.localizedDescription)”)
闭包(PlacesResult.error(错误))
}
如果let likelihoodList=placeLikelihoods{
对于似然度列表中的似然度。似然度{
let annotation=GMAnnotation()
让地点
annotation.location=CLLocation(纬度:place.coordinate.lation,经度:place.coordinate.longitude)
annotations.append(annota