2015-11-04 54 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,你的錯誤是莫名其妙的。