Access control in Swift with example
Access control in Swift:
Access control is based on modules and source files. It is used to set the accessibility (visibility) of classes, structs, enums, properties, methods, initializers, and subscripts.
Types of Access Control in Swift:
There are mainly five types of access controls in Swift programming:
1. open: It makes it possible to inherit a class/override a function outside of the module.
2. public: A class that’s declared public can be reused in other apps. Its declarations are accessible from everywhere.
3. private: Private is the most restrictive access control in Swift. A class that’s declared private can be used only in the file in which it’s defined. Its declarations are accessible only within the defined class or struct. We don’t reuse this class outside.
4. fileprivate: It allows a property/function visible within the whole file, but not outside of it. Its declarations are accessible only within the current swift file.
5. internal: Internal is the default access control if you do not provide one. A class that’s declared internal can be used only by other codes in the same project. Its declarations are accessible only within the defined module (default).