Swift provides fine-grained access control, allowing you to specify the visibility that your code has to the other areas of code. This enables you to be explicit about the interface you provide to other parts of the system, encapsulating implementation logic and helping to 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 on its use
These can be applied to types, properties, and functions...