Swift 3: The difference between Public and Internal access modifiers?

  • Internal - This is default access specifier in swift. With this we can access data members and member functions in the same module (target).

  • Public - This is where you can access all data members and member functions within same module and outside of it. But you can't subclass or override outside the module.

  • Open - same as public, only difference is you can subclass or override outside the module.

  • Fileprivate - As the name say's, data members and member functions are accessible within the same file.

  • Private - This is where you can have access within the scope of function body or class.


The The Swift Programming Language book from Apple clearly explains these access modifiers:

“Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.

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 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 access restricts the use of an entity to the enclosing declaration. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. https://itun.es/gb/jEUH0.l”


Whatever you marked as public can be used within your app and outside of your app(module). If you marked something as internal that can only be used within your app(module). This is very helpful when you're developing a library (framework), you can use internal to hide library structure.