web-dev-qa-db-ja.com

Swift 2 CLLocationManagerエラー更新

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

20
markutus

ちょうどあなたと同じ問題がありました、変更

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

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

プロジェクトをクリックします-[ビルドフェーズ]> [ライブラリとバイナリをリンク]に移動し、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で動作する私のコードです。また、コードをクリーンアップする必要がありました(Product-> Clean)。

2
user2643679

クラスまたはメソッドを@objc属性でマークする必要があります。だからどちらか:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate {

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

}

または:

class MyManagerDelegate: CLLocationManagerDelegate {

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

}
1
akashivskyy

私は次のコードでこれを解決しました:

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