Declaration Is Only Valid at File Scope (Extension)

About Declaration is only valid at file scope

The error is somewhere in your ... — that error means that your ListViewController class didn't get closed, so the extension is being interpreted as nested inside, like this:

class ListViewController {
...
extension ListViewController {
}
}

Find the missing closing brace and you should solve the problem.

IOS SWIFT: Declaration is only valid at file scope

The extension must be at the root level - don't embed them into a class or whatever. So just write:

import UIKit

extension String {
var doubleValue: Double {
...
}
}

extension String {
func doubleValueT() -> Double {
...
}
}

Note that you can also combine them into a single extension:

import UIKit

extension String {
var doubleValue: Double {
...
}

func doubleValueT() -> Double {
...
}
}

Declaration is only valid at file scope how to fix?

Thats because you have declared an extension inside your class. Move it outside the class and compiler will stop complaining

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")
}
}

You have clearly confirmed to CLLocationManagerDelegate in your ViewController extension so you dont need to confirm it in class declaration (this will result in compiler giving you a warning that duplicate confirmation to protocol) so change class ViewController: UIViewController, CLLocationManagerDelegate to class ViewController: UIViewController

Declaration is on valid at file scope (UI color extension)

Is a previous block perhaps missing a close curly brace? If a preceding class or extension is still open, then this extension isn't at file scope – it's one level deeper, which is invalid. I'd double-check that all of your preceding scopes are closed as you expect.



Related Topics



Leave a reply



Submit