2015-06-25 23 views
20

tôi đã cập nhật Xcode 6 lên Xcode 7 beta bằng Swift 2. Tôi gặp lỗi này và tôi không thể tìm ra cách sửa lỗi, vui lòng giúp tôi. Cảm ơn. Đây là mã của tôi:Swift 2 CLLocationManager Lỗi cập nhật

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

và tôi nhận được lỗi này:

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

Trả lời

39

Chỉ có vấn đề tương tự như bạn, thay đổi

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

để

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

'AnyObject' là siêu kiểu của' CLLocation'. Cả hai sẽ hoạt động. – akashivskyy

+1

Tôi biết điều đó nhưng khi tôi thay đổi từ [AnyObject] thành [CLLocation] thì lỗi đã biến mất. –

+0

đã thử rằng, nó hoạt động nhưng sau đó nó đã cho tôi một lỗi trên dòng này: 'let location = locations.last as! CLLocation' nói rằng locations.last không thể là một CLLocation – markutus

1

Bạn cần phải đánh dấu lớp hoặc phương pháp của bạn với @objc thuộc tính. Vì vậy, một trong hai:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate { 

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

} 

Hoặc:

class MyManagerDelegate: CLLocationManagerDelegate { 

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

} 
+1

nhưng tôi có mã khác trong lớp đó được mã hóa trong Swift .. – markutus

2

Bấm vào dự án của bạn - đi Xây dựng các giai đoạn-> Liên kết nhị phân với các thư viện và thêm CoreLocat 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)") 

} 

Đây là mã của tôi mà hoạt động trên xcode 7, tôi cũng đã có để làm sạch mã của tôi (chế biến tiêu thụ> sạch)

0

tôi giải quyết này bằng cách làm theo mã:

//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) 
} 
Các vấn đề liên quan