2015-11-04 52 views
0

看到以前的帖子,但没有解决方案为我工作。Swift 2 CLLocationManager错误

我收到此错误:

Objective-C method 'locationManager:didUpdateLocations:' provided by method 'locationManager(_:didUpdateLocations:)' conflicts with optional requirement method 'locationManager(_:didUpdateLocations:)' in protocol '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() 
} 
+0

只是为了确认,你有没有包括coreLocation框架'进口CoreLocation' – PK20

+0

投票关闭,因为不可重现 – matt

回答

0

您的问题不能被复制。这是完成我的机器上的iOS项目中的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,你的错误是莫名其妙的。