创建全局CLLocationManager时出错

时间:2017-07-14 09:57:07

标签: ios swift

Apple文档建议不要将CLLocationManager存储在本地变量中。所以我在ViewController语句之后创建了一个超出import类范围的全局常量。但是,尝试访问类中的常量会引发编译器错误:

Xcode example screenshot

类似globalDictionary类型的NSMutableDictionary常量似乎可以在类中访问。

我在这里做错了什么? 为什么以上不起作用?

代码

//
//  ViewController.swift
//  CoreLocationExample
//
//

import UIKit
import CoreLocation
import Foundation

let locationManager = CLLocationManager()
let globalDictionary = NSMutableDictionary()
class ViewController: UIViewController, CLLocationManagerDelegate {

//    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        let dict = [
            "name":"John"
        ]
        globalDictionary.addEntries(from: dict)
        print(globalDictionary)
    }
}

在Xcode版本8.3.1上使用Swift 3

3 个答案:

答案 0 :(得分:1)

Apple Docs建议不要将CLLocationManager存储在本地变量中。 这意味着不要在方法/函数中创建本地实例。

请这样声明。

class AppDelegate:UIResponder,UIApplicationDelegate {

//CLLocation Manager
let locationManager = CLLocationManager()
var locValue = CLLocationCoordinate2D()

}

CLLocation Manager或globalDictionary在类中。

答案 1 :(得分:0)

为此

使用singleton类

导入UIKit 导入CoreLocation

public class LocationManager:NSObject,CLLocationManagerDelegate {

public static let sharedInstance = LocationManager()
public lazy var locationManager: CLLocationManager = CLLocationManager()
var globalDictionary = NSMutableDictionary()

public override init() {
    super.init()
    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.startUpdatingLocation()
    self.locationManager.delegate = self
    let dict = [
        "name":"John"
    ]
    self.globalDictionary.addEntries(from: dict)
    print(self.globalDictionary)
}

// MARK: - Location Manager Delegate
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

    print(error)
}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    let newLocation: CLLocation? = locations.last
    let locationAge: TimeInterval? = -(newLocation?.timestamp.timeIntervalSinceNow)!
    if Double(locationAge!) > 5.0 {
        return
    }
    if Double((newLocation?.horizontalAccuracy)!) < 0 {
        return
    }
}

public  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

}

}

答案 2 :(得分:-1)

您可以通过以下方式进行操作 -

class LocationTracker {
static var locationManager: CLLocationManager? = LocationTracker.sharedLocationManager()

class func sharedLocationManager() -> CLLocationManager {

    let lockQueue = DispatchQueue(label: "self")
    lockQueue.sync {
        if _locationManager == nil {
            _locationManager = CLLocationManager()
            _locationManager?.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            _locationManager?.allowsBackgroundLocationUpdates = true
            _locationManager?.pausesLocationUpdatesAutomatically = false
        }
    }
    return _locationManager!
}

}

当您需要在代码中调用它时,您可以执行以下操作 -

var locationManager: CLLocationManager? = LocationTracker.sharedLocationManager()

然后,您可以在此课程中添加其他与位置相关的方法,例如用于开始跟踪,更新位置,停止跟踪等。