2015-06-25 14 views
20

ich aktualisierte Xcode 6 zu Xcode 7 Beta mit Swift 2. Ich bekomme diesen Fehler und ich kann nicht herausfinden, wie man es beheben kann, bitte helfen Sie mir. Vielen Dank. Dies ist mein Code:Swift 2 CLLocationManager Fehler beim Aktualisieren

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

und ich bekomme diese Fehlermeldung:

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

Antwort

39

Gerade hatte das gleiche Problem wie Sie, ändern

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

zu

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

'AnyObject' ist ein Überbegriff von' CLLocation'. Beides sollte funktionieren. – akashivskyy

+1

Ich weiß, aber als ich es von [AnyObject] zu [CLLocation] änderte, ging der Fehler weg. –

+0

schon versucht, dass es funktioniert, aber dann gab es mir einen Fehler in dieser Zeile: 'Lassen Sie location = locations.last as! CLLocation' sagte, dass locations.last kein CLLocation sein kann – markutus

1

Sie benötigen eine Klasse oder Methode mit @objc Attribut zu markieren. Also entweder:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate { 

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

} 

Oder:

class MyManagerDelegate: CLLocationManagerDelegate { 

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

} 
+1

aber ich habe anderen Code in dieser Klasse, die in Swift codiert sind. – markutus

2

Klicken Sie auf Ihr Projekt - gehen Sie zu Erstellen Sie Phasen-> Binary mit Bibliotheken verknüpfen und fügen Sie CoreLocat hinzu ion.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)") 

} 

Dies ist mein Code, der auf Xcode funktioniert 7, auch habe ich meinen Code reinigen (Product-> Clean)

0

Ich löste dies durch folgenden Code:

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