我有一个全局共享类,用于使某些数据可从我的应用程序中的任何位置使用。我一直试图实现的一件事是跟踪用户位置并将这些坐标保存在此全局共享类中。
我尝试实现所有必需的组件,这些组件在旧类中已经使用过,但是这样做时,我被迫添加一些协议存根,但我不确定该如何处理。
另外,我在func
自己() -> Self {
行上遇到错误
这是代码:
extension GlobalSharedData: CLLocationManagerDelegate {
func isEqual(_ object: Any?) -> Bool {
<#code#>
}
var hash: Int {
<#code#>
}
var superclass: AnyClass? {
<#code#>
}
func `self`() -> Self {
<#code#>
}
func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>! {
<#code#>
}
func perform(_ aSelector: Selector!, with object: Any!) -> Unmanaged<AnyObject>! {
<#code#>
}
func perform(_ aSelector: Selector!, with object1: Any!, with object2: Any!) -> Unmanaged<AnyObject>! {
<#code#>
}
func isProxy() -> Bool {
<#code#>
}
func isKind(of aClass: AnyClass) -> Bool {
<#code#>
}
func isMember(of aClass: AnyClass) -> Bool {
<#code#>
}
func conforms(to aProtocol: Protocol) -> Bool {
<#code#>
}
func responds(to aSelector: Selector!) -> Bool {
<#code#>
}
var description: String {
<#code#>
}
可以这样做吗?如果可以,怎么办?
答案 0 :(得分:2)
似乎您错过了NSObject
的子类化
import Foundation
import CoreLocation
protocol LocationServiceDelegate {
func tracingLocation(currentLocation: CLLocation)
func tracingLocationDidFailWithError(error: NSError)
}
class LocationSingleton: NSObject,CLLocationManagerDelegate {
var locationManager: CLLocationManager?
var lastLocation: CLLocation?
var delegate: LocationServiceDelegate?
static let shared:LocationSingleton = {
let instance = LocationSingleton()
return instance
}()
override init() {
super.init()
self.locationManager = CLLocationManager()
guard let locationManagers=self.locationManager else {
return
}
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManagers.requestAlwaysAuthorization()
locationManagers.requestWhenInUseAuthorization()
}
if #available(iOS 9.0, *) {
// locationManagers.allowsBackgroundLocationUpdates = true
} else {
// Fallback on earlier versions
}
locationManagers.desiredAccuracy = kCLLocationAccuracyBest
locationManagers.pausesLocationUpdatesAutomatically = false
locationManagers.distanceFilter = 0.1
locationManagers.delegate = self
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else {
return
}
self.lastLocation = location
updateLocation(currentLocation: location)
}
@nonobjc func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
locationManager?.requestAlwaysAuthorization()
break
case .authorizedWhenInUse:
locationManager?.startUpdatingLocation()
break
case .authorizedAlways:
locationManager?.startUpdatingLocation()
break
case .restricted:
// restricted by e.g. parental controls. User can't enable Location Services
break
case .denied:
// user denied your app access to Location Services, but can grant access from Settings.app
break
default:
break
}
}
// Private function
private func updateLocation(currentLocation: CLLocation){
guard let delegate = self.delegate else {
return
}
delegate.tracingLocation(currentLocation: currentLocation)
}
private func updateLocationDidFailWithError(error: NSError) {
guard let delegate = self.delegate else {
return
}
delegate.tracingLocationDidFailWithError(error: error)
}
func startUpdatingLocation() {
print("Starting Location Updates")
self.locationManager?.startUpdatingLocation()
// self.locationManager?.startMonitoringSignificantLocationChanges()
}
func stopUpdatingLocation() {
print("Stop Location Updates")
self.locationManager?.stopUpdatingLocation()
}
func startMonitoringSignificantLocationChanges() {
self.locationManager?.startMonitoringSignificantLocationChanges()
}
// #MARK: get the alarm time from date and time
}
要使用
LocationSingleton.shared.delegate = self //optional inside the vc
LocationSingleton.shared.startUpdatingLocation() // start this as earlier as possible so when you check of lastLocation is could have a value