Access Control in Swift

Manasa M P
4 min readFeb 10, 2021

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.

Advantages:

  • Ability to clearly define a public API for your code
  • Hide private implementation details

The 5 Access Levels In Swift

  1. open and public — anyone can access, within the module, and in code that imports the module
  2. internal — anyone can access, but only within the module (default)
  3. fileprivate — anyone can access, but only within the current Swift file
  4. private — only the enclosing declaration can access, such as a class or struct.

The default access level in Swift is internal .

Some important points on open and public access control

  • public classes and class members can only be subclassed and overridden within the defining module (target where they’re defined).
  • open classes 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.
  • Even UIKit is considered as one module. For example, when you try to interact with UIComponents such as UITableView, UIButton, UIViewController, and so on, you have to import the UIKit library/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 :-

  1. 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 private or fileprivate

This would fail since variable a has more access than the class A

b. It can be solved by using private for setter as well.

This would fail since the setter has more access than the property and its getter

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.

An overridden function cannot be private.

5. The access level of a Swift Tuple would be the level of the most restrictive element among the elements present.

solution: use private for tuple variable

6. Functions in swift, like Tuples, have the access level equal to the most restrictive level among the parameters and the return type.

solution: use private or fileprivate for function in class B or set class B as private

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’

If you enjoyed reading this post, please share and clap 👏🏻👏🏻👏🏻👏🏻👏🏻, it help others to find it . Follow me for more update. Thank you!!!

--

--