Access controls are usually asked when you’re in the middle of your technical interview. Interviewers want to know if you can identify important parts of your code and whether you can make sure those code are hidden or displayed relative to the other parts of your code base.
What are Access Controls in Swift?
Swift provides five different types of Access Controls in code. These access levels limit access to your code pieces from code in modules and other files. They are related to the source file that any entity can identify.
- Open: This is where you can access all data members and member functions in the same module and outside it. You can subclass or override the module outside.
- Public: Similar to Open. The only difference is that you cannot subclass or override outside the module.
- Internal: This is the default access level in Swift, it allows all data members and member functions to be accessed only in the same module and restrict access outside the module.
- Private: This is where you can access data members and functions in its attached declarations as well as extensions in the same file. It does not allow access in subclass with same file or different file
- File-private: Same as Private. The only difference is that it allows subclass access with the same file.
What is the default access level in Swift?
If we don’t mention an access level before an entity, internal will be the default. It has been explained in the section above.
The interviewer will usually ask you about the differences, for example:
- Open vs Public
- Public vs Internal
- Private vs Fileprivate
Explain the difference between Open and Public
Public you cannot subclass Public outside of the module, but Open you can inherit or override a method.
When using Public while creating a class inside a framework, we cannot inherit that class in another module, as shown below:
Now, we import NetworkFramework and try to subclass NetworkFramework to LoginNetworkManager
Similarly, when using public methods in another module, it will report a compiler error.
As we can see the compiler error. To get rid of this error, we will have to modify access from public to open . Now it should work fine.
You may be asked to write code like in the example above during the interview.
Explain the difference between Internal vs Public?
Public – allows data members to be used in any source file from their specified module, and also in a source file from another module that imports the specified module.
Internal – allows entities to be used in any source file from their specified module, but not to be used in a source file outside the module.
When should the internal access level be used?
When we want to create a framework without allowing their classes or entities to be accessed from another framework or module, internal plays an important role. This is explained more clearly below.
Now, if we try to use this class in any other module it will generate compiler error as shown:
Explain the difference between Private vs Fileprivate
We should explain the difference using the example below
private – Allow data members and functions to access and operate in attached declarations as well as extensions in the same file.
Case 1: In the same source file , if the property or function is declared private in the class – then the scope is by default just the class and the extension of that class.
Can we access data members in subclass?
No, the subclass does not allow access to defined superclass inside the same sourcefile as shown above.
Case 2: In another source file , If a property or function is declared private in a source file and access in extension / subclass in another source file – access is not allowed
So here we have seen the compiler error which clearly says “properties are inaccessible due to private protection level”, now we can solve this by using fileprivate . See an example:
fileprivate – Allows you to access data members and functions in the same source file or in a subclass or extension
Case 1: In the same source file , If we create an extension or subclass in the same class file and try to access the fileprivate entity in the extension / subclass – permissions are allowed
So, as seen in the private level, there was an error creating subclass in the same source file here, we solved that error with data members declared as fileprivate .
Case 2: In another source file, fileprivate works like private , for example below:
Here, the compiler error is generated when the fileprivate says you can access data members in the same file they were declared, while private says you can only access data members in the declaration scope. newspaper and attached extensions.
Where to apply access levels?
Can the interviewer ask this question in a way that we can use in enum or struct?
- Classes, structs, enumerations, protocols
- Properties, functions, computed properties and subscripts
- Custom types, and nested types