Access Control in Swift
It is used to restrict parts of your app’s source code. This essentially helps for prohibiting other modules, frameworks, classes and code from using your code.
- Ability to clearly define a public API for your code
- Hide private implementation details
The 5 Access Levels In Swift
public— anyone can access, within the module, and in code that imports the module
internal— anyone can access, but only within the module (default)
fileprivate— anyone can access, but only within the current Swift file
private— only the
enclosingdeclaration can access, such as a class or struct.
The default access level in Swift is
Some important points on open and public access control
publicclasses and class members can only be subclassed and overridden within the defining module (target where they’re defined).
openclasses and class members can be subclassed and overridden both within and outside the defining module (target where they’re defined).
If you aren’t too clear with what a module is?
- Imagine a module is a bundle of code. Your single Xcode project/framework/bundle is considered as a single module.
UIKitis considered as one module. For example, when you try to interact with UIComponents such as
UIViewController, and so on, you have to import the
UIKitlibrary/module on the top.
You can use access control for function, class member, struct, enum, tuple, property or any other characteristic
Rules which need to follow while using Access control :-
- A function/property or any characteristic of class/struct/enum/protocol cannot have a higher access level than its defined access level.
Refer above image for access level hierarchy.
a. It can be solved by using access level which is lesser than internal i.e
b. It can be solved by using private for setter as well.
2. A subclass cannot have a higher access level than it’s superclass.
solution: change the super class to higher access level than subclass or change the sub class to lower access level than super class
3. Overridden methods can have a higher access level than their superclass methods.
4. A private function cannot be overridden.
5. The access level of a Swift Tuple would be the level of the most restrictive element among the elements present.
6. Functions in swift, like Tuples, have the access level equal to the most restrictive level among the parameters and the return type.
7. Protocol access level must be set at the time of defining that protocol. The protocol functions/requirements must have the same access level defined.
Just like Subclassing, if a protocol inherits from another protocol, it cannot have a higher access level that the inherited one.
8. The access level for an enumeration would be applied across all its cases
9. You cannot declare a private/fileprivate class with an extension of public or internal or open
10. The access level for a generic type or generic function is the minimum of the access level of the generic type or function itself and the access level of any type constraints on its type parameters.
11. A type alias can have an access level less than or equal to the access level of the type it aliases.
For example, a private type alias can alias a private, file-private, internal, public, or open type, but a public type alias can’t alias an internal, file-private, or private type.
12. Only classes and overridable class members can be declared ‘open’; use ‘public’