What are the Access Controllers in Swift?

Lakshmi K
3 min readApr 21, 2022

--

Swift Access Control

In most of the interviews, interviewer will ask this question , so let’s go deep dive…

We have 5 keys where they will restrict access of classes and class members upto some extension. Those keys are

  1. Open
  2. Public
  3. internal
  4. fileprivate
  5. private

open and public are mostly same apart from one or two features , like wise fileprivate and private are also mostly same , just one or two features are different. Let’s see one by one with an example

Open:

Open is accessible from the same module which is defined and also accessible from another module which will import this module. Little bit confusing ? :(. Let’s see an example

  1. We created a module name “SqliteFree”(just assumption name)
  2. If you are getting confused with what is module ?, module is nothing but which we will import on top of swift file using import key

ex: import AVFoundation (AVFoundation is module developed by Swift Organisation)

Like wise we can also create our own modules,(creating module is for another topic which I will cover later).

3. Now in SqliteFree module I created on open class , I can use this class and class members within SqliteFree module without any restrictions.

4. Likewise if I import SqliteFree module into my swift file and I can use the open class and open class members in anywhere in that file , I can extend the class , I can sub class the open class , I can override open class members like methods, like this I have no restrictions on open class and open class members.

Public:

So what could be the issues with open class ?

If there is a situation like you have one class and class methods in SqliteFree module , where user can use them but can’t modify them , he simply use it as it is without subclassing the classes or overriding the methods.In this scenario we can take advantage of public access controller.

By using public you can do everything open can do , apart from these things

  1. If we write public class in SqliteFree module , and then I imported that module into my current project , I can use public classes and class members as it as. We can’t subclass or we can’t override public class memebers.
  2. By this we can restrict modification access to few classes and class members

Internal:

If we want to restrict access to classes and class members to only that particular module , not outside of the module, then we can use internal access controller.

For example I want to implement access of one class within SqliteFree module for internal purpose , so whoever imported this module won’t get any access to these classes and class members, in this kind of scenarios we use internal access.

Fileprivate:

If any members of class declared as fileprivate and private , we can access those inside that swift file only , outside of that swift file we can’t access it.

By using fileprivate we can access those class members by extending the class , sub classing the class and we can override the class methods within the same swift file.

If same class is extended or subclassed in another swift file we can’t access fileprivate variables

Private:

Before swift 4 private key has different meaning which is if anything is declared as private it will only accessible within those braces of the function or class.

after that private key got new features like it is accessible within the class and extension class only within in the same swift file, but we can’t override private members within same file or another file.

Below is the example for fileprivate and private

Please go through my other articles , thanks for reading. Happy Coding and Reading :).

https://medium.com/@kn.lakshmi948/what-is-protocol-oriented-programming-pop-in-swift-with-examples-c9b358d13f65

--

--