Swift Access Control

In Swift, access controls are used to set the accessibility (visibility) of classes, structs, enums, properties, methods, initializers, and subscripts. For example,

class Vehicle {

  public func method1() {...}
  private func method2() {...}
}

Here,

  1. method1() is public i.e. it can be accessed by other classes.
  2. method2() is private i.e. it can not be accessed by other classes.

Note the keyword public and private. These are access controls in Swift.


Types of Swift Access Controls

In Swift, there are four access controls:

Controls Description
public declarations are accessible from everywhere
private declarations are accessible only within the defined class or struct
fileprivate declarations are accessible only within the current swift file
internal declarations are accessible only within the defined module (default)

public Access Control

In Swift, when methods, properties, classes, and so on are declared public, then we can access them from anywhere.

The public access modifier has no scope restriction. For example,

class Animal {

// public property public var legCount: Int = 0 // public method public func display() { print("I am an animal."); print("Total Legs:", legCount) }
} // create an object var obj = Animal() // access and assign value to public property obj.legCount = 4 // access the public method obj.display()

Output

I am an animal.
Total Legs: 4

In the above example, we have created a class named Animal with two public data members: legCount and display().

We have then created an object of Animal class named obj. We then access the public data members directly by using the codes obj1.legCount and obj1.display().


private Access Control

When we declare a type member as private, then it can only be accessed within the same class or struct. For example,

class Student {

// private property private var name = "Tim Cook" // private method private func display() { print("Hello from Student class") }
} // create object of Student class var student1 = Student() // access name property print("Name:", student1.name) // access display() method student1.display()

In the above example, we have created a class named Student with a property name and a method display(). Notice the statements,

// access name property
print("Name:", student1.name)

// access display() method
student1.print();

Since name and display() are marked as private, we are not able to access them outside Student. Here, the code will generate the following error.

error: 'name' is inaccessible due to 'private' protection level
error: 'display' is inaccessible due to 'private' protection level

fileprivate Access Control

When we declare a type member as fileprivate, then it can only be accessed within the defined source file. For example,

class Student {

// fileprivate property fileprivate var name = "Tim Cook" // fileprivate method fileprivate func display() { print("Hello from Student class") } }
// create object of Student class var student1 = Student() // access name property print("Name:", student1.name) // access display method student1.display()

Output

Name: Tim Cook
Hello from Student class

In the above example, we have created the fileprivate data members name and display() inside the Student class.

Since the name and display() are marked as fileprivate, these data members are accessible from anywhere in the source file where it is defined.

Note: If we create another Swift file and try to access the fileprivate data members, we'll get an error.


internal Access Control

When we declare a type or type member as internal, it can be accessed only within the same module.

A module is a collection of types (classes, protocols, etc) and resources (data). They are built to work together and form a logical unit of functionality.


Example: internal Within The Same Module

class Student {

// define internal property internal var name = "Tim Cook"
} // create object of Student class var student1 = Student() // access name property print("Name:", student1.name)

Output

Name: Tim Cook

In the above example, we have created a class named Student with a property name. Since name is internal, we are able to access it outside the class as they are in the same module.

If we use internal within a single module, it works just like the public access modifier.

Note:

  • If we create another module and try to access the internal data members, we'll get an error.
  • To learn how to create a module, visit Swift Module.
Did you find this article helpful?