Swift 2 CLLocationManager 更新错误

Swift 2 CLLocationManager Error updating

我使用 Swift 2 将 Xcode 6 更新为 Xcode 7 beta。我遇到了这个错误,我不知道如何修复它,请帮助我。谢谢。这是我的代码:

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
    let location = locations.last as! CLLocation

    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.map.setRegion(region, animated: true)
}

我收到这个错误:

Objective-C method 'locationManager:didUpdateLocations:' provided by method 'locationManager(_:didUpdateLocations:)' conflicts with optional requirement method 'locationManager(_:didUpdateLocations:)' in protocol 'CLLocationManagerDelegate'

您需要用 @objc 属性标记您的 class 或方法。所以要么:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate {

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
        ...
    }

}

或者:

class MyManagerDelegate: CLLocationManagerDelegate {

    @objc func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
        ...
    }

}

刚遇到和你一样的问题,换

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

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

单击您的项目 - 转到构建阶段->Link 带有库的二进制文件并添加 CoreLocation.framework

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var LatitudeGPS = NSString()
var LongitudeGPS = NSString()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    updateLocation()

func updateLocation() {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    //self.locationManager.distanceFilter = 10
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

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

    locationManager.stopUpdatingLocation() // Stop Location Manager - keep here to run just once

    LatitudeGPS = String(format: "%.6f", manager.location!.coordinate.latitude)
    LongitudeGPS = String(format: "%.6f", manager.location!.coordinate.longitude)
    print("Latitude - \(LatitudeGPS)")

}

这是我的代码,适用于 xcode 7,而且我确实必须清理我的代码(产品->清理)

我通过以下代码解决了这个问题:

//CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
    let location:CLLocation = locations[locations.count-1] as! CLLocation 

    if (location.horizontalAccuracy > 0) {
        self.locationManager.stopUpdatingLocation()
        print(location.coordinate, terminator: "")
        updateWeatherInfo(location.coordinate.latitude, longitude: location.coordinate.longitude)
    }
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last as! CLLocation!
    manager.stopUpdatingLocation()
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001))
    mapView.setRegion(region, animated: true)
}