Hacking The iOS Interview

Swift — Access Control

Neha Bansal
Hash Coding
Published in
7 min readMar 3, 2021

--

Access controls are mostly asked when you are halfway through your technical interview. The interviewer wants to know whether you can identify critical pieces in your code and whether you ensure that those code pieces are hidden or exposed accordingly to other parts of your codebase.

What are Access Controls in Swift?

Swift provide five different types of access controls within your code. These access levels restrict access to parts of your code from code in other source files and modules. These are relative to the source file in which any entity can define(entity e.g properties, methods, types and so on, and module e.g which can be shipped as a single unit in an application such as target or any framework).

  • OpenThis is where you can access all data members and member functions within the same module(target) and outside of it. You can subclass or override outside the module(target).
  • PublicThis is the same as open, the only difference is you can’t subclass or override outside the module(target).
  • InternalThis is the default access level in Swift, it allows all data members and member functions to be accessed within the same module(target) only and restrict access outside the module(target).
  • Private — This is where you can access data members and function within its enclosing declaration as well as an extension within the same file. It does not allow access in a subclass with in the same file or in another file.
  • File-private - This is the same as private, the only difference is it allows access in a subclass with in the same file.

What is the default access level in swift?

If we don’t mention an access level before any entity, it is internal by default. It’s explained in the above section.

Access Modifier Comparison

The interviewer generally ask you the differences between various access modifiers, for example:

  • open vs public
  • public vs internal
  • private vs fileprivate

Explain the difference between Open vs Public — (least restrictive)

public - you cannot subclass public entity outside the module(target) but in open you can inherit a class or override a method.

When using public modifier while creating a class inside a framework, we can not inherit that class inside another module(target) as can be seen in the below given example:

public access modifier example in Swift
NetworkManager.swift class inside a framework

Now In the below example, we have NetworkFramework imported in the app’s target and we try to subclass the NetworkManager into loginNetworkManager

Public access modifier gives an error when trying inherit a class outside the module
LoginNetworkManager.swift class in a separate module(module can be app target or any other framework)

Similarly, while using public methods in another module it will give the compiler error:

LoginNetworkManager.swift class in a separate module with the public function

As we can see the compiler gives an error. To remove this error we will have to upgrade the access modifier from public to open. Now it will work fine.

You can be asked to write the code as shown in the above example in the interview.

Explain the difference between Internal vs Public?

Public — access enables data members to be used within any source file from their defining module, and also in a source file from another module that imports the defining module

Internal — access enables entities to be used within any source file from their defining module, but not in any source file outside of that module.

When should we use the internal access level?

When we want to create a framework in which we don’t want to allow classes and their entities to be accessed from other framework or module then internal plays an important role. This can be explained more clearly to the interviewer using an example like the one given below:

NetworkManager.swift class inside the framework with default access level

Now, if we try to use this class in any other module then it will generate compiler error as shown:

LoginNetworkManager.swift class in any other target

Explain the difference between Private vs Fileprivate — (most Restrictive)

We should explain the difference using the below cases with a real example:

private — Allow access data members and function within its enclosing declaration as well as an extension within the same file

Case 1: Within the same source file, if property or function is declared as private in class — then the scope is by default the class and extension of that class only.

NetworkManager.swift which declared properties and function as private access level

So, can we access data members inside the subclass?

No, the subclass is not allowed to access those data members defined in the superclass within the same source file as shown above.

Case 2: In the different source file, If property or function is declared as private in one source file and access within extension/subclass in another source file — access not allowed

NetworkUtility.swift file which creates an extension of NetworkManager class and accesses private data members here

So here, we have seen the compiler error which clearly says “properties are inaccessible due to private protection level” now we can resolve this error by using fileprivate. Let’s see an example:

filePrivate — Allow access data members and functions within the same source file either in subclass or extensions

Case 1: Within the same source file, If we create an extension or subclass with in the same class file and try to access fileprivate entity in its extension/Subclass — access allowed

NetworkManager.swift class with properties and functions declared as file private and accessed in subclass and extensions.

So as seen in the private access level there was an error while created subclass within the same source file here we have resolved that error by declared data members as fileprivate.

Case 2: In the different source file, fileprivate behaves the same as the private modifier as shown in the below example:

Here, the compiler error is generated as fileprivate says that you can access the data members within the same file in which they are declared while private says that you can only access data members within enclosing declaration scope and extensions.

Where can we apply access levels?

The interviewer can ask this question in a way that can we use modifiers in enum or struct?

  • Classes, structs, enumerations, protocols
  • Properties, functions, computed properties and subscripts
  • Custom types, and nested types

How do Access levels work with tuple?

Tuple types don’t have a standalone definition in the way that classes, structures, enumerations, and functions do. A tuple type’s access level is determined automatically from the types that make up the tuple type, and can’t be specified explicitly.

Let’s see the example in which we try to show you that tuple considered as one of the properties in the class.

NetworkManager.swift class which declared tuple as private access level

It clearly says that all the member inside tuple determined automatically with the same type which declared in the property

The below question can be asked in multiple-choice type questions:

For example, if you compose a tuple from two different types, one with internal access and one with private access then what will be the access level of that tuple?

The access level for that compound tuple type will be private.

Because the most restrictive access level is applied in that case to compound tuple so in this case private is the most restrictive access level.

I hope you find this story helpful. If you enjoyed it, share this with your community and feel free to hit the clap button below 👏 to help others find it! Thanks for reading. 👍

Get notified every time we post a new story to our publication. Follow us now

We are dedicated to our goal to help every iOS developer grow and give their best in the interview. Every week we’ll be coming up with newer topics. Don’t miss them. Slide to Subscribe Now. ✉️

--

--