Swift 2 CLLocationManager错误

时间:2015-11-04 23:31:20

标签: swift swift2 cllocationmanager cllocation

看过上一篇文章,但没有一个解决方案适合我。

我收到此错误:

  

Objective-C方法'locationManager:didUpdateLocations:'由提供者提供   方法'locationManager(_:didUpdateLocations :)'与。冲突   可选的需求方法'locationManager(_:didUpdateLocations :)'   在协议'CLLocationManagerDelegate'

使用以下代码:

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: 1, longitudeDelta: 1))
    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()
}

1 个答案:

答案 0 :(得分:0)

您的问题无法复制。这是我机器上iOS项目中Swift文件的完整内容;它在Swift 2.1下非常愉快地编译:

import UIKit
import CoreLocation
import MapKit
class MapViewController : UIViewController, CLLocationManagerDelegate {
    var mapView : MKMapView!
    var locationManager : CLLocationManager!
    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: 1, longitudeDelta: 1))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()
    }
}

当然,该代码实际上不会做任何事情(没有实际的地图视图);它只是一些代码。但是没有编译器错误。

如果您仍在使用早期版本的Swift,请更新您的Xcode副本。如果您使用的是Xcode 7.1,则您的错误是无法解释的。