Access specifiers across dynamic frameworks and apps – Swift iOS

Access control defines how to allow access to parts of your code from code in other files and modules/frameworks. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used.You can assign specific access levels to individual types like classes, properties, methods, initializers, etc. Swift provide default code protection for code within your framework. Here in this article we can discuss briefly on the scope of these access specifiers.


Swift provides 5 access specifiers to protect and share the functionality of your Frameworks/public SDK to vendors or within the company apps.
I have shared a client app and framework project – “accessspecifiers-1” below depicting the usage of access controls and can be used to play around with access specifiers for more understanding.

Let’s discuss the specifiers in the ascending order of protection.

Open
Open is used in modules/frameworks where a class is defined with a purpose for others to subclass and override. Most commonly used when you define a base class which handles provides default functionalities that other subclasses can use.
You can think UIViewController is Open class which we use to subclass all the view controllers. All the lifecycle methods like viewwillappear , viewdidappear are all open methods which has default behaviors and can also be overridden.

open class OpenClass: NSObject {
  open func testOpenAccess() {
  }
}

Public
Public classes/methods are used to share functionality across modules/Frameworks but public classes cannot be subclassed outside of the framework/module the class is defined.

public class PublicClass: NSObject {
   public func testPublicAccess() {
       print("PublicClass - testPublicAccess")
   }
}

Internal – Default access specifier
Internal is the access specifier provided by swift if there is none specified explicitly. All the internal classes are available to subclass/access within the framework/module.
Internal classes cannot be accessed outside the framework/module.

FilePrivate
This access specifier is restricted the file within your framework/Module. The same cannot be accessed outside of the file.

Private
This access specifier is restricted to the scope defined file n a framework/module.
Simple code snippet depicting the access specifiers.

//Class can be access only within framework
class OtherAccessClass: NSObject {
    func testInternalAccess() {
           print(" InternalClass - \(String(describing: self))")
        testPrivateAccess()
        testFilePrivateAccess()
    }
    
    //Can be access within the scope enclosed
    private func testPrivateAccess() {
        print(" testPrivateAccess - \(String(describing: self))")
        testInternalAccess()
        testFilePrivateAccess()
    }
}

//Can be accessed within the file enclosed
fileprivate func testFilePrivateAccess() {
      print(" testFilePrivateAccess")
}

Bonus tip:
Protected – There is no Access specifier “protected” in swift language. Apple considers any protected method or class falls under 2 categories(private/Internal) in swift iOS world.

Leave a comment