首页 > 解决方案 > 为什么我不能使用 manager.locationServicesEnabled()

问题描述

我正在检查应用程序是否启用了位置服务。查看下面的代码,为什么我不能使用“!manager.locationServicesEnabled()”,因为 locationManager 是 CLLocationManager 类型的?

override func viewDidLoad() {
    super.viewDidLoad()

    enableLocationServices(manager: locationManager)

}

func enableLocationServices(manager: CLLocationManager) {

    manager.delegate = self

    if !CLLocationManager.locationServicesEnabled() {
        manager.requestWhenInUseAuthorization()
    }
}

标签: iosswiftcore-location

解决方案


假设您要问为什么必须使用CLLocationManager.locationServicesEnabled()而不是manager.locationServicesEnabled()那么答案很简单,那locationServicesEnabled就是类型方法并且必须在类上调用,而不是类的实例。

在查看方法或属性的文档时,如果它以classstatic然后直接在类、结构或枚举上调用它。如果它不以classor开头static,那么您在类、结构或枚举的特定实例上调用它。


推荐阅读