Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建swift帮助器类以处理CoreLocation函数_Swift_Cllocationmanager - Fatal编程技术网

创建swift帮助器类以处理CoreLocation函数

创建swift帮助器类以处理CoreLocation函数,swift,cllocationmanager,Swift,Cllocationmanager,我是Swift的新手,我已经成功创建了一个入门应用程序,可以获取当前的GPS位置。现在我正在尝试创建一个助手类(Location.swift),它将执行所有基于GPS位置的函数,并且可以从我的主ViewController调用(例如Location.getGPSLocation(),其中getGPSLocation是我的Location.swift助手类中的静态函数) 请查看我的Location.swift helper类代码: import Foundation import

我是Swift的新手,我已经成功创建了一个入门应用程序,可以获取当前的GPS位置。现在我正在尝试创建一个助手类(Location.swift),它将执行所有基于GPS位置的函数,并且可以从我的主ViewController调用(例如Location.getGPSLocation(),其中getGPSLocation是我的Location.swift助手类中的静态函数)

请查看我的Location.swift helper类代码:

    import Foundation
    import CoreLocation

    public class Location
    {
    private static let locationManager = CLLocationManager()

    public static func getGPSLocation()
    {
        let authorizationStatus = CLLocationManager.authorizationStatus()

        if (authorizationStatus == .notDetermined)
        {
            locationManager.requestWhenInUseAuthorization()
            return
        } else if (authorizationStatus == .denied || authorizationStatus == .restricted)
        {
            print("ERROR: Please enable location services")
            return
        } else
        {
            startLocationManager()
        }

    }

    private static func startLocationManager()
    {
        if CLLocationManager.locationServicesEnabled()
        {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    }
}
我现在收到一条错误消息,上面的代码是“无法将“Location.type”类型的值分配给“CLLocationManagerDelegate”类型?”

我怎样才能避开这件事


干杯

首先,您的
位置
对象需要继承
NSObject
CLLocationManagerDelegate
。其次,您需要提供一个共享的实例属性,并将manager声明从静态更改为实例属性。第三,覆盖您的
位置
初始值设定项,调用super,然后启用您的位置服务,并在那里设置经理代表:

import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {

    private let manager = CLLocationManager()

    static let shared = Location()

    var location: CLLocation?

    private override init() {
        super.init()
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestWhenInUseAuthorization()
            manager.distanceFilter = 10
        }
    }

    static func requestGPS() {
        Location.shared.manager.requestLocation()
    }
}
现在,您可以实现扩展位置的委托方法:

extension Location {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.location = locations.last
        print("location", location ?? "")
    }
    func locationManager(_ manager: CLLocationManager,
                                  didChangeAuthorization status: CLAuthorizationStatus) {
        print(#function, "status", status)
        // check the authorization status changes here
    }
    func locationManager(_ manager: CLLocationManager,
                                  didFailWithError error: Error) {
        print(#function)
        if (error as? CLError)?.code == .denied {
            manager.stopUpdatingLocation()
            manager.stopMonitoringSignificantLocationChanges()
        }
    }
}

首先,您的
Location
对象需要继承
NSObject
CLLocationManagerDelegate
。其次,您需要提供一个共享的实例属性,并将manager声明从静态更改为实例属性。第三,覆盖您的
位置
初始值设定项,调用super,然后启用您的位置服务,并在那里设置经理代表:

import CoreLocation
class Location: NSObject, CLLocationManagerDelegate {

    private let manager = CLLocationManager()

    static let shared = Location()

    var location: CLLocation?

    private override init() {
        super.init()
        if CLLocationManager.locationServicesEnabled() {
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestWhenInUseAuthorization()
            manager.distanceFilter = 10
        }
    }

    static func requestGPS() {
        Location.shared.manager.requestLocation()
    }
}
现在,您可以实现扩展位置的委托方法:

extension Location {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.location = locations.last
        print("location", location ?? "")
    }
    func locationManager(_ manager: CLLocationManager,
                                  didChangeAuthorization status: CLAuthorizationStatus) {
        print(#function, "status", status)
        // check the authorization status changes here
    }
    func locationManager(_ manager: CLLocationManager,
                                  didFailWithError error: Error) {
        print(#function)
        if (error as? CLError)?.code == .denied {
            manager.stopUpdatingLocation()
            manager.stopMonitoringSignificantLocationChanges()
        }
    }
}

谢谢你对单身班的反馈。谢谢你对单身班的反馈。