Swift 2 CLLocationManager错误更新

Swift 2 CLLocationManager错误更新,cllocationmanager,swift2,xcode7,xcode7-beta2,Cllocationmanager,Swift2,Xcode7,Xcode7 Beta2,我用Swift 2将Xcode 6升级为Xcode 7测试版。我得到这个错误,我无法找到如何修复它,请帮助我。谢谢这是我的代码: func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) { let location = locations.last as! CLLocation let center = CLLocationCoordinate2D(

我用Swift 2将Xcode 6升级为Xcode 7测试版。我得到这个错误,我无法找到如何修复它,请帮助我。谢谢这是我的代码:

 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方法“locationManager:didUpdateLocations:”由方法“locationManager(u:didUpdateLocations:)”提供与协议“CLLocationManagerDelegate”中的可选需求方法“locationManager(uu:didUpdateLocations:)”冲突。
您需要使用
@objc
属性标记类或方法。因此,要么:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate {

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

}
或:


只是和你有同样的问题,改变

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


单击您的项目-转到构建阶段->将二进制文件链接到库并添加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)
    }
}

AnyObject
CLLocation
的超类型。两者都应该可以工作。我知道,但是当我将它从[AnyObject]更改为[CLLocation]时,错误消失了。已经尝试过了,它可以工作,但是它在这一行给了我一个错误:
let location=locations.last as!CLLocation
说locations.last不能是CLLocation我通过执行
让location=locations.last as!来修复它!CLLocation我喜欢在斯坦福iOS课程中,保罗·赫加蒂(Paul Hegarty)抱怨“AnyObject”超类型,现在苹果在Swift 2中修复了它P
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)")

}
//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)
}