2015-12-03 34 views
0

因此,我正在制作一个显示地图和当前位置的应用程序。现在我做了一个按钮,当我按下按钮时,它应该在我的位置上做一个标记(注释)。Swift返回值

所以我有这个函数抓住我的当前位置,以便我可以在地图上显示它。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    let location = locations.last 

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.mapView.setRegion(region, animated: true) 

    self.locationManager.stopUpdatingLocation() 

    return center 
} 

现在我想在另一个函数中使用变量“Center”的数据。但是当我“返回中心”时。我得到以下错误:“Unexpected non-void return value in void function

我搜索了很多,我搜索了堆栈溢出。但我很快就有了新的发现。似乎无法找到或理解如何解决它。

我希望有人能帮助我,向我解释我该如何解决这个问题!

提前致谢!

回答

1

既然你想返回center这是一个CLLocationCoordinate2D,你的函数的签名必须明确地返回一个CLLocationCoordinate2D,像这样的:在签名

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) -> CLLocationCoordinate2D { 

    let location = locations.last 

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.mapView.setRegion(region, animated: true) 

    self.locationManager.stopUpdatingLocation() 

    return center 
} 

没有-> CLLocationCoordinate2D,假设该函数返回void,因此你得到的错误信息。