Access Levels in Swift

private, file-private, public, internal, open

Pınar Koçak
3 min readJul 7, 2022

“Access control restricts access to parts of your code from code in other source files and modules. 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.”

Swift programming language has 5 different access specifiers. In order of restrictions from least restrictive to most restrictive:

  • open
  • public
  • internal
  • fileprivate
  • private

In the case we declare a property, a class or etc. we do not need to write access specifier. In default, access specifier is set to internal. You can assign specific access levels to individual types such as classes, structures, enumerations, properties, methods, initializers, and subscripts.

Why we use them? Code reusability and security, which are the two main concepts of access levels.

Now let’s take a closer look at each of them.

public:

The less restrictive access level. By making an entity public you make it accessible by other parts of code inside the same module, and to other modules as well. For example, you have a library integrated into a project as a Swift Package; entities that should be available to the project out of the library must be marked as public, otherwise they won’t be accessible. More about that later.

open:

Similar to public, but it provides an extra “freedom”.

open class is accessible and subclassable outside of the defining module. Otherwise public class is accessible but not subclassable outside of the defining module. Also, open class and public class member is accessible outside of the defining module. And open class member is overridable outside of the defining module but public class member is not overridable.

Codes from UIKit.UIView

internal:

This is the default one that is given automatically to all entities that they have not been marked otherwise.

“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. You typically use internal access when defining an app’s or a framework’s internal structure.”

file-private:

This is a most strict access level after private. Entities marked as file-private are accessible from any other entity only inside the same source file, but not outside of it.

“File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.”

private :

Important: Before swift 4, private access level didn’t allow the use of a class member inside the extension of same class.

This is the most strict access level. None of these entities can be accessed by parts of code being outside their defining type or file.

“Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.”

I recommend reading the Swift language guide chapter on access control.

See you in next post…

--

--