Swift | Access Control

Ahmet Enes Irmak
3 min readJul 26, 2023

--

Hello everyone, today I will talk about Access Control. Access Control is one of the most fundamental topics in Swift. In this article, we will learn what “access control” elements do and how they are used. Also, we will explain these elements by giving examples.

Let’s get started 🥳

What is Access Control?

With Access Control, we can impose restrictions on the use of classes, modules, methods and, variables in our projects. In this way, we allow our classes, methods, and variables to be accessed and changed as we need. For example, we have variables and functions in our class. We want them to be accessible only within that class. we can provide this with the access control elements.

There are 5 different access control elements in the Swift programming language. These elements are as follows from most restrictive to least restrictive.

  • Private
  • File Private
  • Internal
  • Public
  • Open

Access Levels

“Private” Access Control

Private is the most restrictive access modifier. Through Private, we can access the variable, function, etc. for using in a class. We can not access it from another class. So we can only access from within the same class. In addition, if we define a class as private, we cannot inherit that class.

Let’s examine the example below. We have a class called Game and inside this class we have a property called game_name and a method called start_game(). This property and method are marked as private. Therefore, it is not possible to use it outside of its own class. However, in the example below, our property and method are used outside of our class and this is an incorrect representation.

Due to the incorrect representation in the example above, we get the following error on lines 18 and 21.

‘ game_name’ is inaccessible due to ‘private’ protection level

“File Private” Access Control

The File Private access modifier only allows access within the same file. What is meant by the file here are; Swift Class, Cocoa Touch Class etc. In other words, we can access from different classes and structs in the same file.

To better understand this Access Control element, let’s examine the following example. As in the Private example, we have a class called Game. In this class, we have a property named game_name marked as “fileprivate” and a method called start_game(). We can access this property and method from outside of our class. Therefore, the following example is a correct representation for the fileprivate access modifier.

If we create a different swift file and try to access a data that we marked as fileprivate, we will get an error.

“Internal” Access Control

Internal is the default access modifier. Classes, modules, methods and variables are defined as internal when we do not specify any properties. We cannot access the Internal access modifier from a different module.

“Public” Access Control 👨🏻‍💻

Public is one of the least restrictive access modifiers. The public access modifier has no scope restrictions. We can access classes, methods, and properties defined as “public” from anywhere. However, when we access a public method from a different module, we cannot override it. This is the most obvious difference between the Open and Public access modifiers.

“Open” Access Control

Open is one of the least restricted access modifiers. It is also very similar to the public access modifier. We can access classes, methods, and properties defined as “open” from anywhere. In addition, we can override open methods when it is desired to be accessed through another module.

Thank you for reading up to here 😋. This is my third article about Swift. I hope it will be understandable and useful. With these articles, I’m learning many subjects more deeply. I also enjoy sharing what I’ve learned with you.

I look forward to your suggestions and recommendations.

Good coding everyone.✌🏻👨🏻‍💻

References

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

https://www.programiz.com/swift-programming/access-control

https://www.mobilhanem.com/swift-access-control/

--

--