Access control in Swift

Don’t let your client misuse your classes

Steven Curtis
Swift Coding

--

Access control works to restrict assess from parts of your code, enabling you to hide implementation details of code and specify a prefered interface through which code can be accessed.

Prerequisites:

  • Some experience of OO concepts might be useful

Terminology

Module: A framework or application built and shipped as a single unit that can be imported by another module with Swift’s import keyword. Equivalent to node modules, packages, gems or jars in other languages.

Source file: A .swift file within a Swift program

Access control

Swift has 4 levels of access control, and the explanation here is followed up with examples in the next section.

These are listed from least restricted to most restricted, and here is the list:

Open access — entities can be used within any source file from their defining module. Used when specifying the public interface to a framework. Open class code is subclassable. Additional access level as Swift discourages inheritence. Objects outside my framework can subcass this.

Public access — public class members cannot be subclassed from outside their…

--

--