Sitemap

Member-only story

Access Control in Swift

3 min readJul 22, 2024

--

Getting confused about the open, public , private keywords in swift? No worries. You are in the right place. Lets dive into the content without wasting time. As the name suggests, access control restricts or enables access to some part of your code from the code in other other modules or files.

Types of access specifiers :

  1. Open : Using this keyword, you can access the properties and functions within the same module and also outside the module. Subclassing is possible outside the module.

Lets see this with an example. I have created a new project to explain this.
I have added a new package named “AccessControl”. Below is the image.

Press enter or click to view image in full size
Open access specifier Fig 1

Now, I create a class named TestAccessibility to test the accessibility outside the module.

Press enter or click to view image in full size
Open access specifier Fig 2

Here, as you can see we are able to extend the “AccessControl” class (which is inside the package) from outside the module and override the “getAccessSpecifierType” method. The compiler does not throw any error.

2) Public : The difference between public and open is we can access the properties and methods from outside the module but we will not be…

--

--

Manikandan S
Manikandan S

Written by Manikandan S

An iOS enthusiast who loves to develop scalable iOS applications.