gpt4 book ai didi

ios - CLLocationManager 和 CLLocationManagerDelegate 的问题

转载 作者:行者123 更新时间:2023-11-28 11:45:20 24 4
gpt4 key购买 nike

我正在运行 Xcode 10 和 iOS 12

我在 CLLocationManager 单例的类扩展中编码的每个委托(delegate)方法上都收到此警告:

实例方法“locationManager(:didChangeAuthorization:)”几乎匹配协议(protocol)“CLLocationManagerDelegate”的可选要求“locationManager(:didChangeAuthorization:)”

代码如下:

import Foundation
import CoreLocation

public class PhysicalLocationManager: NSObject {

/*--------------------------------------------------------------------------------*/
//MARK: - Create Singleton Shared Instance
/*--------------------------------------------------------------------------------*/
static let sharedInstance: PhysicalLocationManager = {
let instance = PhysicalLocationManager()
return instance
}()

let locationMgr: CLLocationManager

/*--------------------------------------------------------------------------------*/
//MARK: - Initialization
/*--------------------------------------------------------------------------------*/
override init() {
locationMgr = CLLocationManager()
locationMgr.distanceFilter = kCLDistanceFilterNone
locationMgr.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
super.init()
locationMgr.delegate = self
}

func enableBasicLocationServices() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
locationMgr.requestWhenInUseAuthorization()
break

case .restricted, .denied:
// Disable location features
// TODO: disableMyLocationBasedFeatures()
break

case .authorizedWhenInUse, .authorizedAlways:
// Enable location features
enableWhenInUseFeatures()
break
}
}

func enableWhenInUseFeatures() {
locationMgr.startUpdatingLocation()
if CLLocationManager.locationServicesEnabled() {
locationMgr.requestLocation()
}
}

}

extension PhysicalLocationManager: CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("\(manager)\tCLLocationManager, didChangeAuthorization\n\(status)")

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("\(manager)\tCLLocationManager, didUpdateLocations\n\(locations)")
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
// locationManager.stopUpdatingLocation()
}

}

谁能看出我在这里做错了什么?

谢谢,

最佳答案

因为您的 PhysicalLocationManager 类是公共(public)的,所以委托(delegate)方法也需要是公共(public)的。只需在三个委托(delegate)方法前添加 public,警告就会消失。

关于ios - CLLocationManager 和 CLLocationManagerDelegate 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52710252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com