Swift provides fine-grained access control, allowing you to specify the visibility that your code has to other areas of code. This enables you to be deliberate about the interface you provide to other parts of the system, thus encapsulating implementation logic and helping separate the areas of concern.
Swift has five access levels:
- Private: Only accessible within the existing scope (defined by curly brackets) or extensions in the same file.
- File private: Accessible to anything in the same file, but nothing outside the file.
- Internal: Accessible to anything in the same module, but nothing outside the module.
- Public: Accessible both inside and outside the module, but cannot be subclassed or overwritten outside of the defining module.
- Open: Accessible everywhere, with no restrictions in terms of its use, and can therefore be subclassed and overwritten.
These can be applied to types, properties, and functions.