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
Does Swift Have a Null Coalescing Operator and If Not, What Is an Example of a Custom Operator
Checking If an Array of Custom Objects Contain a Specific Custom Object
How to Open Your App in Settings iOS 11
Swift Arrays and Contains, How to Determine If a Collection Contains an Object or Value
How to Disable Vertical Scroll in Tabview with Swiftui
How to Convert a String to a Cstring in the Swift Language
How to Execute Code Once and Only Once in Swift
Only First Track Playing of Avmutablecomposition()
Cannot Invoke 'Join' with an Argument List of Type (String, [String]) in Swift 2.0
What Determines Whether a Swift 5.5 Task Initializer Runs on the Main Thread
Swift Map(_:) Extension for Set()
Grab Frames from Video Using Swift
"Unrecognized Selector Sent to Instance" in Swift
In Swift, Can You Split a String by Another String, Not Just a Character