Access specifier in Swift

Control your code’s accessibility

ANil Kotur
Analytics Vidhya
3 min readJan 31, 2020

--

What is Access specifier?

Access specifier is keyword which helps in Access control of code block. Access control restricts access to the parts of your code from code in other source files and modules.

Encapsulation is one pillar of Object Oriented Programming and access specifiers helps in encapsulating.

What is Encapsulation?

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

Types of Access levels in swift

1. Open & Public :

Enable an entity to be used outside the defining module (target/framework). You typically use open or publicaccess when specifying the public interface to a framework.

However, open access applies only to classes and class members, and it differs from publicaccess as follows:

  • open classes and class members can be subclassed and overridden both within and outside the defining module (target/framework).
  • public classes and class members can only be subclassed and overridden within the defining module (target/framework).

Like open access level, public access level enable an entity to be used outside the defining module (target). But open access level allows us to subclass it from another module where in public access level, we can only subclass or overridde it from within the module it is defined.

2. Private:

Restricts the use of an entity to its enclosing declaration. You typically use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

3. FilePrivate:

Restricts the use of an entity to its defining source file. You typically use fileprivate access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.

4. Internal:

Enables an entity to be used within the defining module (target). You typically use internal access when defining an app’s or a framework’s internal structure.

Note: Default access specifier in swift is Internal, Then how about final let’s read this article.

Thank you for reading 👏🏻. do like and share to make this reach more engineers.

--

--