ios - 为什么我不能使用 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()
}
}
解决方案
假设您要问为什么必须使用CLLocationManager.locationServicesEnabled()
而不是manager.locationServicesEnabled()
那么答案很简单,那locationServicesEnabled
就是类型方法并且必须在类上调用,而不是类的实例。
在查看方法或属性的文档时,如果它以class
或static
然后直接在类、结构或枚举上调用它。如果它不以class
or开头static
,那么您在类、结构或枚举的特定实例上调用它。
推荐阅读
- python - datetime.strptime 的替代方法,适用于重复的格式元素
- java - OkHttp 异步调用返回 JsonArray Null
- android - 如何使用 WorkManager 和 OneTimeWorkRequest 在 Android 中的特定时间接收每日通知?
- docker - 启用了 selinux 的 Docker - 不允许在 /usr 中重新标记内容
- jspdf - 如何使用 jsPDF 和 jSPDF autoTable 在特定单元格中绘制圆角矩形?
- c++ - 即使 QFilterKey 匹配,QRenderPassFilter 也会隐藏平面网格
- javascript - 查看 Apple Pay 价格
- react-native - 反应导航不响应单击按钮
- java - 如何为文件处理问题编写代码
- .net - .net cad:从另一个图纸导入尺寸样式