我在运行Xcode 10和iOS 12
我在CLLocationManager singleton的类扩展中编码的每个委托方法上都会收到此警告:
实例方法“locationManager(:didChangeAuthorization:)”几乎与协议“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
,警告就会消失。