2015-08-23 27 views
0

我試圖在地圖視圖上顯示和跟蹤位置。我得到的錯誤:在地圖視圖上使用軌道位置時出現可變錯誤

Use of local Variable 'locationManager' before declaration

的錯誤是對的LocationManager的所有行:

locationManager.delegate = self 
locationManager.desiredAccuracy = kCLLocationAccuracyBest 
locationManager.requestAlwaysAuthorization() 
locationManager.startUpdatingLocation() 

我已經ViewDidLoad之前聲明var locationManager = CLLocationManager()

我有我的變數在錯誤的地方嗎?

import UIKit 
import MapKit 
import CoreLocation 

class InfoViewController: UIViewController,UIGestureRecognizerDelegate, CLLocationManagerDelegate { 


    // Outlets 
    @IBOutlet weak var infoImageView: UIImageView! 


    @IBOutlet weak var nameLabel: UILabel! 


    @IBOutlet weak var categoryLabel: UILabel! 


    @IBAction func urlButton(sender: UIButton) { 
    } 

    @IBOutlet weak var mapView: MKMapView! 

    // Variables 

    var gesture: UISwipeGestureRecognizer! 

    var store: Store! 

    var locationManager = CLLocationManager() 




    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     // Background image 
     self.infoImageView.image = UIImage(data: store.photo) 

     // Set label text 
     self.nameLabel.text = store.name 

     // Set category text 
     var categoryText = String() 
     for category in store.storeCategories { 
      if !categoryText.isEmpty { 
       categoryText += ", " 
      } 
      categoryText += category.name 
     } 

     categoryLabel.text = categoryText 




     if (CLLocationManager.locationServicesEnabled()) 
     { 

      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestAlwaysAuthorization() 
      locationManager.startUpdatingLocation() 

     } 

     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.mapView.setRegion(region, animated: true) 
      } 

     // Gesture recoginizer 
     gesture = UISwipeGestureRecognizer(target: self, action: "dismissView") 
     self.gesture.direction = UISwipeGestureRecognizerDirection.Up 

     self.view.addGestureRecognizer(self.gesture) 

    } 

    func dismissView() { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 


} 
+0

哪條線出現該錯誤?將有助於顯示整個文件,以便我們可以看到'locationManager'的聲明位置。 – NickEntin

+0

謝謝@NickEntin我得到所有位置管理器變量的錯誤。我更新了原始問題中的代碼。你可以看一下嗎? – amym

回答

1

您必須聲明的LocationManager塊之外,在這種情況下,你已經嘗試聲明它的if-block這將給使它成爲一個局部變量中。聲明if-block.工作示例代碼以外的變量爲作業:

var locationManager = CLLocationManager() 

override func viewDidLoad() { 

正如你所看到的,我已經宣佈在同一水平viewDidLoad()方法的變量,給它一個更更大的範圍。

,然後在你的,如果塊,你可以這樣做:

locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 
    locationManager.startUpdatingLocation() 

請注意,我刪除了locationManager = CLLocationManager()第一線,因爲它已經被實例化。

此外,locationManager的函數不能位於viewDidLoad方法內。

此外,我不明白爲什麼你的if-block是包裝的方法。

+0

謝謝。現在讓我覺得我不應該在視圖中調用func locationManager。感謝你幫助我理解。地圖工作很棒:) – amym

+0

@amym請確保你選擇了正確的答案。 –

+0

@amym老兄你是否會打勾答覆? –

0

正如Hassan所說,您只需要將locationManager(...)函數聲明移出if塊。要認識到的重要一點是,在Swift中,可以像變量一樣傳遞函數的引用。因此,locationManager(...)函數的聲明(將被稱爲locationManager)將覆蓋變量的聲明。

現在您的代碼應該是這樣的:

import UIKit 
import MapKit 
import CoreLocation 

class InfoViewController: UIViewController,UIGestureRecognizerDelegate, CLLocationManagerDelegate { 


    // Outlets 
    @IBOutlet weak var infoImageView: UIImageView! 


    @IBOutlet weak var nameLabel: UILabel! 


    @IBOutlet weak var categoryLabel: UILabel! 


    @IBAction func urlButton(sender: UIButton) { 
    } 

    @IBOutlet weak var mapView: MKMapView! 

    // Variables 

    var gesture: UISwipeGestureRecognizer! 

    var store: Store! 

    var locationManager = CLLocationManager() 




    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     // Background image 
     self.infoImageView.image = UIImage(data: store.photo) 

     // Set label text 
     self.nameLabel.text = store.name 

     // Set category text 
     var categoryText = String() 
     for category in store.storeCategories { 
      if !categoryText.isEmpty { 
       categoryText += ", " 
      } 
      categoryText += category.name 
     } 

     categoryLabel.text = categoryText 

     if (CLLocationManager.locationServicesEnabled()) 
     { 

      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestAlwaysAuthorization() 
      locationManager.startUpdatingLocation() 
     } 

     // Gesture recoginizer 
     gesture = UISwipeGestureRecognizer(target: self, action: "dismissView") 
     self.gesture.direction = UISwipeGestureRecognizerDirection.Up 

     self.view.addGestureRecognizer(self.gesture) 

    } 

    func dismissView() { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 

    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.mapView.setRegion(region, animated: true) 
    } 

} 

此外,當您設置此對象的委託,預計該方法是從類直接訪問。同樣,在Swift中,當涉及範圍時,方法就像變量一樣。所以當你在另一個方法中聲明時,它不能從類中訪問。當你在課堂上直接聲明時,可以調用delegate.locationManager(...)

+0

感謝您的詳細解釋。幫助我讓我的頭靠近它。現在效果很好。感謝您的幫助:) – amym

+0

快樂,我可以幫助:) – NickEntin

相關問題