【问题标题】:Swift 2 CLLocationManager Error updatingSwift 2 CLLocationManager 错误更新
【发布时间】:2015-09-11 17:42:01
【问题描述】:

我使用 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'

【问题讨论】:

    标签: cllocationmanager swift2 xcode7 xcode7-beta2


    【解决方案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)
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        点击您的项目 - 转到 Build Phases->Link Binary With Libraries 并添加 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 上工作的代码,我也必须清理我的代码(产品->清理)

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

          • AnyObjectCLLocation 的超类型。两者都应该工作。
          • 我知道,但是当我将它从 [AnyObject] 更改为 [CLLocation] 时,错误消失了。
          • 已经尝试过了,它可以工作,但它在这一行给了我一个错误:let location = locations.last as! CLLocation 说locations.last 不能是CLLocation
          • 我通过let location = locations.last as! CLLocation!修复它
          • 我喜欢在斯坦福的 iOS 课程中,Paul Hegarty 抱怨“AnyObject”超类型,现在 Apple 在 Swift 2 中修复了它。:P
          【解决方案5】:

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

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

          或者:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-04-22
            • 2023-02-16
            • 2014-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多