Access Control levels in SWIFT iOS

Abhimuralidharan
6 min readMar 22, 2017

source: Apple docs ,stackoverflow and of course medium.com.

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.

You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types. Protocols can be restricted to a certain context, as can global constants, variables, and functions.

In Swift 3 and swift 4, we have open, public, internal, fileprivate, and private for access control.Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

Almost all entities in your code have a default access level of internal if you do not specify an explicit access level yourself. As a result, in many cases you do not need to specify an explicit access level in your code.

As of Swift 4, there are 5 levels of access, described below from the highest (least restrictive) to the lowest (most restrictive).

1. open and public — (least restrictive)

Enable an entity to be used outside the defining module (target). You typically use open or public access when specifying the public interface to a framework.

--

--