[英]Endless loop with when passing self as delegate
I am creating a wrapper around CLLocationManager
but am struggling to initialize the wrapper LocationManager
in my view controller. 我正在围绕CLLocationManager
创建包装器,但正在努力在我的视图控制器中初始化包装器LocationManager
。
My view controller does the following: 我的视图控制器执行以下操作:
class MyVC: UIViewController, CLLocationManagerDelegate {
var locationManager: LocationManager? {
get {
if self.locationManager == nil {
self.locationManager = LocationManager(delegate: self)
}
return self.locationManager
}
set {
self.locationManager = newValue
}
}
...
}
The idea is to initialize a LocationManager
only when it is actually needed. 想法是仅在实际需要时才初始化LocationManager
。 However I'm receiving a EXC_BAD_ACCESS(code=2...
and what looks like an endless loop on my main thread as the code breaks at the if self.locationManager == nil { ...
~175000 times before crashing. My LocationManager
takes my view controller as a CLLocationManagerDelegate as an argument, passing it along to the actual manager. 但是我收到一个EXC_BAD_ACCESS(code=2...
什么样子我的主线程无限循环截至代码休息if self.locationManager == nil { ...
〜崩溃之前175000次。我LocationManager
将我的视图控制器作为CLLocationManagerDelegate作为参数,并将其传递给实际的管理器。
import Foundation
import CoreLocation
class LocationManager {
let delegate: CLLocationManagerDelegate
var locationManager: CLLocationManager? {
get {
if self.locationManager == nil {
let locationManager = CLLocationManager()
locationManager.delegate = self.delegate
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
self.locationManager = locationManager
}
return self.locationManager
}
set {
self.locationManager = newValue
}
}
init(delegate: CLLocationManagerDelegate) {
self.delegate = delegate
}
func start() {
if self.isAuthorized() {
self.locationManager!.startUpdatingLocation()
}
}
func stop() {
self.locationManager!.stopUpdatingLocation()
}
func isAuthorized() -> Bool {
switch CLLocationManager.authorizationStatus() {
case .Authorized:
return true
default:
return false
}
}
func requestAuthorization() {
self.locationManager!.requestAlwaysAuthorization()
}
}
Is use of self
not allowed to be passed like this in a computed property? 是否不允许在计算属性中像这样传递self
使用?
What you want is a lazy property like this 您想要的是一个像这样的懒惰属性
lazy var locationManager: LocationManager = {
let manager = LocationManager(delegate: self)
return manager
}()
Also set locationManager
in LocationManager
class to lazy property. 还要将LocationManager
类中的locationManager
设置为lazy属性。
lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.delegate = self.delegate
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
return locationManager
}()
Your error is here 您的错误在这里
var locationManager: LocationManager? {
get {
if self.locationManager == nil {
self.locationManager = LocationManager(delegate: self)
}
return self.locationManager
}
set {
self.locationManager = newValue
}
}
You call self.locationManager
in the get method of locationManager
,it will fall in dead loop 您在locationManager
的get方法中调用self.locationManager
,它将陷入死循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.