Access Control in Swift iOS
There are scenarios in programming where we need to restrict access to some specific part of our code from an unwanted part or module. In swift, we can implement this by using Access Control which can restrict the access to the parts of our code from an unwanted code in other source files and modules. Access Control facilitates us to hide the implementation details of our code. This allows us to specify a platform that can access the code and use it.
In this article, we will discuss how to specify different access levels to individual types, including classes, structures, and Enums. We can provide different access levels to swift value and reference types such as Classes, Structures, and Enums. We can also assign access levels to their properties, methods, initializers, and subscripts.
swift also provides default access levels to individual types so that we don't need to assign various levels of access control explicitly for each type.
What are Modules and Source Files?
Swift provides Access Control based on modules and source files. The module is the part of our code that can be grouped. It can be reused in different places of our code without writing the code again. In XCode, each build target is used as an individual module. The source file contains a source code of an individual swift source code within a module. However, we can define separate types, including classes, structures, functions, Enums, etc., in a source file.
There are five different access levels in swift. Swift categorizes these access levels relative to the source files which contain the entity. It also relative to the module, which includes the source file.
1. Open Access Level
The open-access level allows the entities to be used in any source files in the defining module. It also allows us to use the entity in a source file from another module which imports the defining module. We need to use open access to specify the public interface for a framework.
2. Public Access Level
The public access level allows the entities to be used in any source files in the defining module. It also allows us to use the entity in a source file from another module which imports the defining module. We need to use public access to specify the public interface for a framework.
3. Internal Access Level
The internal access restricts the entities on the module level. It allows the entities to be used in any source file from the defining module, but it can't be used in the source files outside of that module. We use internal access for defining an application's internal structure.
4. File-Private Access Level
The File-Private access restricts the entities on the source file level. It allows the entities to be used in their defining source file. The file-private access is used to hide the implementation details of functionality used within an entire file.
5. Private Access Level
Private access provides the highest level of restriction to an entity. It allows the entities to be used only within the enclosing declaration. It can also be used in the extensions of the declaration that are in the same file. We use private access for the piece of functionality that is defined only within a single declaration.
The open-access level is the least restrictive, and private access is the most restrictive access level. We must notice the difference between public and open access levels. The open-access level can only be applied to classes and class members. It is not similar to the public access level as it allows the code outside the module to subclass and overrides.
Here, we need to ensure that an entity cannot be defined inside (in terms of) an entity with a lower access level. For example, we can't define a public variable having an internal, private, file-private type.
Swift defines all the entities in our code as having internal access level by default. It allows us not to specify an explicit level in the code.
Access Control Syntax
We can use open, public, internal, fileprivate, and private keywords while declaring swift variables.