Start a Project

Access Control in swift

Access Control main purpose is to prevent the access of code in other modules or files. By access control code accessibility can be handle. By using Access control we can assign the specific access level to the class, structure, enumerations, properties, subscripts, methods, initializers.

Types of Access control:-

  1. Open
  2. Public
  3. internal
  4. fileprivate
  5. private

 

1) Open:- It has the highest access level means it is the least restrictive.  It provides access levels from other modules and files.

2) Public:-It is almost similar to open but unlike open which is subclassed and override from other modules. By using the public we can not subclassed and override from defining modules.

Let takes an example to differentiate between Open and Public.

3) internal:- It is default access control. The method or class which declares internal can be accessed from anywhere within the same module.

For Example

4) filePrivate:- The fileprivate gives accessibility within one single swift file.

For Example

5) private:- It has the lowest access level means it is the highest restrictive. It restricts the entity to the enclosing declaration. According to swift doc “Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.”

For example

Thanks for reading the blog.

You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.

Reference

Exit mobile version