Access Control Specifier(Swift) — in a Nutshell
Access Control Specifier(Swift) — in a Nutshell
open, public, internal, private, fileprivate
In this article, I am covering all five access control specifier but in a nut shell, so that you can remember it for long time.
You have to understand one thing: module. Imagine a module is a bundle of code. Your single Xcode project/framework/bundle is considered as a single module. for example: CoreData, UIKit, CustomFramework are modules.
Open:
Subclass from outside module possible(UICollectionView or UITableView), add more features , access its own functions.
Public :
Subclass not possible, Delegate can be used to override the functionality , outside the module(UICollectionViewDelegate or UITableViewDelegate) — It can be only overridden by subclasses only within the module where they’re defined. Public access level enable an entity to be used outside the defining module
Internal:
Access limit to with in the module, example: Internal functions created by CoreData engineers can not be accessed by iOS developers even if we import CoreData.
FilePrivate :
Access from outside its type but in same file. If you mark something filePrivate it can be read anywhere in the same file it was declared — even outside the type.
Private :
Access from outside but only in its type (extension), a private property can only be read inside the type that declared it, or inside extensions to that type that were created in the same file.
IMPORTANT
A public type defaults to having internal members, not public members. If you want a type member to be public, you must explicitly mark it as such. This requirement ensures that the public-facing API for a type is something you opt in to publishing, and avoids presenting the internal workings of a type as public API by mistake.
References: