Swift 中单独的 class 位置管理器
Location Manager in seperate class in Swift
所以我目前正在做一些项目,我遇到了这个问题。如果我在 ViewController 中使用 CLLocationDelegate,它通常 运行s,但是当我尝试将其分离到它自己的 class 中时,它就不起作用了。当我尝试 运行 它只是不 运行 下面的函数。任何建议表示赞赏:)
视图控制器:
import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON
class TodayViewController: UIViewController {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationRequest.init()
}
}
位置管理器class:
import Foundation
import CoreLocation
class locationRequest: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override init() {
print("before super.init()")
super.init()
print("after super.init()")
if CLLocationManager.locationServicesEnabled() {
print("before setting delegate")
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
print("after setting delegate")
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("didUpdate")
if let location: CLLocationCoordinate2D = manager.location?.coordinate {
print(location.latitude)
print(location.longitude)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("didFail")
print(error.localizedDescription)
}
}
首先请以大写字母开头的自定义结构和 classes 命名。
错误发生是因为locationRequest
class没有强引用。
要么将class设计为单例
class LocationRequest: NSObject, CLLocationManagerDelegate {
static let shared = LocationRequest()
...
}
...
class TodayViewController: UIViewController {
var locationRequest = LocationRequest.shared
override func viewDidLoad() {
super.viewDidLoad()
}
或者创建一个惰性实例化属性
class LocationRequest: NSObject, CLLocationManagerDelegate { ... }
...
class TodayViewController: UIViewController {
lazy var locationRequest = LocationRequest()
override func viewDidLoad() {
super.viewDidLoad()
}
}
所以我目前正在做一些项目,我遇到了这个问题。如果我在 ViewController 中使用 CLLocationDelegate,它通常 运行s,但是当我尝试将其分离到它自己的 class 中时,它就不起作用了。当我尝试 运行 它只是不 运行 下面的函数。任何建议表示赞赏:)
视图控制器:
import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON
class TodayViewController: UIViewController {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationRequest.init()
}
}
位置管理器class:
import Foundation
import CoreLocation
class locationRequest: NSObject, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override init() {
print("before super.init()")
super.init()
print("after super.init()")
if CLLocationManager.locationServicesEnabled() {
print("before setting delegate")
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
print("after setting delegate")
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("didUpdate")
if let location: CLLocationCoordinate2D = manager.location?.coordinate {
print(location.latitude)
print(location.longitude)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("didFail")
print(error.localizedDescription)
}
}
首先请以大写字母开头的自定义结构和 classes 命名。
错误发生是因为locationRequest
class没有强引用。
要么将class设计为单例
class LocationRequest: NSObject, CLLocationManagerDelegate {
static let shared = LocationRequest()
...
}
...
class TodayViewController: UIViewController {
var locationRequest = LocationRequest.shared
override func viewDidLoad() {
super.viewDidLoad()
}
或者创建一个惰性实例化属性
class LocationRequest: NSObject, CLLocationManagerDelegate { ... }
...
class TodayViewController: UIViewController {
lazy var locationRequest = LocationRequest()
override func viewDidLoad() {
super.viewDidLoad()
}
}