Access Control — Swift 5 (iOS)

A way to handle the restrictions on your code.

Source: https://unsplash.com/@tma | Edited: Self

1. open — least restricted

2. public — some restriction

IndexPath declared Public in Foundation module

3. internal — default access specifier

4. fileprivate — less accessible

5. private — least accessible

Guiding Force

Declarations of access specifiers

// open
open class Switch: UISwitch
{
open func toggleSate(to: State) { //TODO: Implemention needed }
}
// public
public class Switch: UISwitch
{
var state: State
public func toggleSate(to: State) {
self.state = state
}
}
// internal- by default
enum Size: String
{
case small
case medium
case large
}
// explicit
internal func changeSize(to: Size)
{
...
}
// fileprivate
public class Switch: UISwitch
{
fileprivate typealias Color = UIColor
fileprivate var state: State
fileprivate func toggleSate(to: State) {
self.state = state
}
}
// private
class Switch: UISwitch
{
private typealias Color = UIColor
private var state: State
}

Access levels and their ordering

Relative levels of access controls and their level of restrictions

Thank you for reading!

iOS Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store