声明仅在文件范围内有效如何修复?
Declaration is only valid at file scope how to fix?
我的 swift 文件有问题。
我正在尝试按照 Youtube 上的教程进行操作,但他没有收到错误消息。
这是代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
private var currentLocation: CLLocationCoordinate2D?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
configerLocationServices()
}
private func configerLocationServices() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status == .authorizedAlways || status == .authorizedWhenInUse {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
extension ViewController: CLLocationManagerDelegate { //Error: Declaration is only valid at file scope
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did get latest Lcation")
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("The Status changed")
}
}
}
我不知道我做错了什么,有没有人有解决办法?
提前致谢。
删除 class ViewController: UIViewController, CLLocationManagerDelegate
行中的 CLLocationManagerDelegate
声明。另外,确保正确设置所有花括号对,我的意思是在正确的位置打开和关闭
那是因为您在 class 中声明了一个扩展。将它移到 class 之外,编译器将停止抱怨
class ViewController: UIViewController {
private let locationManager = CLLocationManager()
private var currentLocation: CLLocationCoordinate2D?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
configerLocationServices()
}
private func configerLocationServices() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status == .authorizedAlways || status == .authorizedWhenInUse {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did get latest Lcation")
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("The Status changed")
}
}
您已在 ViewController
扩展中明确确认 CLLocationManagerDelegate
,因此您无需在 class 声明中确认(这将导致编译器警告您重复确认到协议)所以将 class ViewController: UIViewController, CLLocationManagerDelegate
更改为 class ViewController: UIViewController
我的 swift 文件有问题。 我正在尝试按照 Youtube 上的教程进行操作,但他没有收到错误消息。 这是代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
private var currentLocation: CLLocationCoordinate2D?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
configerLocationServices()
}
private func configerLocationServices() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status == .authorizedAlways || status == .authorizedWhenInUse {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
extension ViewController: CLLocationManagerDelegate { //Error: Declaration is only valid at file scope
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did get latest Lcation")
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("The Status changed")
}
}
}
我不知道我做错了什么,有没有人有解决办法?
提前致谢。
删除 class ViewController: UIViewController, CLLocationManagerDelegate
行中的 CLLocationManagerDelegate
声明。另外,确保正确设置所有花括号对,我的意思是在正确的位置打开和关闭
那是因为您在 class 中声明了一个扩展。将它移到 class 之外,编译器将停止抱怨
class ViewController: UIViewController {
private let locationManager = CLLocationManager()
private var currentLocation: CLLocationCoordinate2D?
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
configerLocationServices()
}
private func configerLocationServices() {
locationManager.delegate = self
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
} else if status == .authorizedAlways || status == .authorizedWhenInUse {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did get latest Lcation")
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("The Status changed")
}
}
您已在 ViewController
扩展中明确确认 CLLocationManagerDelegate
,因此您无需在 class 声明中确认(这将导致编译器警告您重复确认到协议)所以将 class ViewController: UIViewController, CLLocationManagerDelegate
更改为 class ViewController: UIViewController