Declaration is only valid at file scope (extension)

The error message is quite clear. You cannot declare an extension inside of anything – inside a class declaration, inside a struct declaration, etc. It must be outside of everything, at the top level of the containing file. There must be no curly braces around it. You have not shown the context in which you are declaring this extension, but clearly there are curly … Read more

What’s the correct way to implement default properties when doing default implementation of protocols in Swift?

It’s been a few weeks since I posted this, but I believe what Aaron Brager said is true. While Protocol-Oriented-Programming is rather new in itself, the idea of protocols has been present in Objective-C for a long time, they are in Swift, and they have their variants in languages such as Java. Due to the … Read more

What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean?

Background: What’s an Optional? In Swift, Optional<Wrapped> is an option type: it can contain any value from the original (“Wrapped”) type, or no value at all (the special value nil). An optional value must be unwrapped before it can be used. Optional is a generic type, which means that Optional<Int> and Optional<String> are distinct types — the type inside <> is called the Wrapped type. Under the hood, … Read more