【发布时间】:2018-12-17 02:41:19
【问题描述】:
我编写了一个可以将位置反转为地理信息的类,如下所示:
import Foundation
import UIKit
import CoreLocation
public class Geocoder{
public static func Geocoder(latitudein:Double,longitudein:Double)->String
{
var address:String?="";
var geocoder = CLGeocoder.init();
var location:CLLocation=CLLocation.init(latitude: latitudein, longitude: longitudein);
// var placemarks:[CLPlacemark]=[CLPlacemark].init();
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
for p in placemarks!
{
if(p != nil)
{
//print(p);
// address?.append(p.country!);
// address?.append(p.subAdministrativeArea!);
address=p.name;
}
}
}
print(address);
return address!;
}
}
但是地址是nil,我试过p.country等很多参数,还是返回nil,请问为什么,怎么解决?
【问题讨论】:
-
您缺少一个概念:异步。
reverseGeocodeLocation()是异步的。如果您放置日志,您会看到print(address);在address=p.name;之前被调用。寻找“Swift + Async + Closure”。 -
如何解决,我是 swift 编程新手
-
不要在 Swift 中使用
;作为行终止符,并且不要以大写字母开头 func 名称
标签: swift location clgeocoder