App development is fun. With free tools available, it’s very easy to translate your idea into an app. For iOS app development, Xcode is a good platform that lets you develop apps based on Swift — Apple’s powerful programming language. However, one common roadblock that most beginners struggle to get through is when Xcode shows “Declaration is only valid at file scope” at the extension line.
This error occurs when you are using Swift extensions. This article will help you decipher what happened and how you can get rid of it forever. You’ll get to read the explanations of experts, as well as proven solutions that you can use. Furthermore, if you have other solutions to the problem, just leave a comment below.
Without further ado, let’s get started!
Swift Extensions and Why Use Them?
As the name suggests, extensions extend or add new functionalities to an existing class, structure, or protocol. It won’t alter the original structure and the existing class, for example, but it’ll add more functionality to it without the need to have access to the original source code. You can read full description – scope and advantage – of extension on Swift right here.
Using extensions on Swift programming allows you to get more out of your code without getting to write more code. It allows you to get rid of subclassing and cluttering in your code. Other advantages of Swift extensions include separating/grouping code, namespaced constants, using protocol conformance and so much more. It’s a very powerful tool to help you write code easily and neatly.
Declaration is Only Valid at File Scope — Investigated & Solved!
Since you are dealing with several blocks and lines of codes, it’s inevitable that you miss something or have conflicts with your initialization and other stuff. Even seasoned programmers are sometimes troubled by the mystery of the missing curly braces. Here are the three most common reasons why you have come across such a problem and how to solve it.
Missing Closing Brace or Incorrect Position
More often than not, if you are working with hundreds of lines already, you might have missed the closing curly brace or you have accidentally deleted it. In the case of the Swift extensions, if you have a missed curly brace or placed it incorrectly, Xcode will treat extensions as a nested code block. Protocols cannot be nested; hence the error “Declaration is only valid at file scope” will surely display.
Solution: First, check the curly braces on your main class code. If one of the closing curly braces is missing, insert it in the code wherein the extensions block will not be nested. Your class and extension should be separated. Whether you’ll write the extension block after class, it depends on your preference. But, there’s a better way to do it.
Extensions Declaration Inside the Class Blocks
If you are writing your extensions after the main class and you miss a closing curly brace, Xcode will construe that extension is part of the class. So, it’ll return the error. However, you can get rid of this by placing all extension blocks before the class.
Solution: The error specifies that “Declaration is only valid at file scope.” This means the declaration should appear outside any function definition or at the root level. Put the extension outside the class block.
And, the safest way to do it is to add the extension right after the import statement, before the class block. With this, there’s no way you’ll encounter the problem even if you missed a closing brace in the class. Furthermore, this also gives the extension a broader scope. It’ll be used not only in the class but the entire project.
Wrapping Source Code for Conversion
Although it’s not a common occurrence, some developers have this problem when trying to convert code to the latest format. Some people suggest wrapping the source file in the public struct. Doing so will hide the complexity of the source and make it compatible with the newer version. Unfortunately, the latest version of Swift doesn’t support wrapping because it has extensions and public protocols, already.
Solution: Don’t do wrapping in a top-struct. Instead, you only need to refer to the global module scope. Furthermore, there’s no need to make changes in the original source code. Learn how extensions can be used in this situation.
The Final Say
Swift programming language is beautiful and powerful. You just have to understand how to use it, otherwise, you’ll end up with an error like, “Declaration is only valid at file scope.” It’s normal to encounter errors and obstructions when writing code. Yet, you should know what causes the error to be able to address it successfully.
You will bump into this error if you are using extensions or if you try to compile a program from an older version of Swift. The most common reason why you’ll get this error instead is the missing closed curly braces on the main class. Especially when you are writing the extension after, if you miss a curly brace, Xcode will assume that extension is part of the main block. Thus, returning an error because extensions couldn’t be nested. Terminating the main class with the closing brace will save you from further trouble.
Another trick for this is to put the extension at the root level, as specified by the error message. It’s a good practice that you’ll create the extension first, then, the main class. No reason that the extension will be considered as a nested code because it’s on top. This will likewise broaden the scope of the extension to be used throughout the project.
Lastly, it could be that you’re wrapping the source file of your code developed with the older version of Swift. Wrapping isn’t supported in the later Swift versions because of the dominance of protocols and extensions.
This should wrap up the article, and we hope this was of help to you. Should the error persist or if you have other workarounds, just leave a comment below.