2015-09-26 56 views
0

我正在将我的应用程序更新为Swift 2.0,但是我遇到了CLLocationManager问题。无法访问不同类别的变量

我已经使用了这段代码了一段时间,所以我有些困惑,为什么它突然成为2.0中的一个问题。我使用一个全局变量(懒惰,我知道),但它似乎并没有在比它在声明的一个以外的任何其它类访问我收到此错误:

Use of unresolved identifier 'locationManager'

这是代码我在课堂,我声明了locationManager

var locationManager = CLLocationManager() 

class InitalViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate { 
     if #available(iOS 8.0, *) { 
     locationManager.requestAlwaysAuthorization() 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.startUpdatingLocation() 

     if CLLocationManager.locationServicesEnabled() { 
      //Requests location use from user for maps 
      locationManager.requestWhenInUseAuthorization() 
     } 
    } 
} 

而且这是在其他类代码:

@IBAction func centerOnLocation(sender: AnyObject) { 
    if locationManager.location != nil { 
     let locationCamera = MKMapCamera() 
     locationCamera.heading = parkPassed.orientation! 
     locationCamera.altitude = 600 
     locationCamera.centerCoordinate.latitude = locationManager.location.coordinate.latitude 
     locationCamera.centerCoordinate.longitude = locationManager.location.coordinate.longitude 

     mapView.setCamera(locationCamera, animated: true) 
    } 
} 

人有什么想法?

回答

0

您可以实现的CLLocationManager的扩展,使用实例为单例。

extension CLLocationManager{ 

    class var sharedManager : CLLocationManager { 
    struct Singleton { 
     static let instance = CLLocationManager() 
    } 
    return Singleton.instance 
    } 
} 

那么你就可以访问该单中,如果这可能是这种情况我已经不知道任何类

@IBAction func centerOnLocation(sender: AnyObject) { 
    let locationManager = CLLocationManager.sharedManager 
    if locationManager.location != nil { 
     let locationCamera = MKMapCamera() 
     locationCamera.heading = parkPassed.orientation! 
     locationCamera.altitude = 600 
     locationCamera.centerCoordinate.latitude = locationManager.location.coordinate.latitude 
     locationCamera.centerCoordinate.longitude = locationManager.location.coordinate.longitude 

     mapView.setCamera(locationCamera, animated: true) 
    } 
} 
0

全局默认访问级别现在为internal,如同一个模块中所有源文件的内部一样。如果centerOnLocation是在不同的模块,您需要将public改性剂添加到您的全球性的定义:

public var locationManager = CLLocationManager() 
+0

,但我仍然得到同样的错误很遗憾。 – user3746428