Swift — Access Control

1. open and public — (least restrictive)

Enable an entity to be used outside the defining module (target). You typically use open or public access when specifying the public interface to a framework.

@available(iOS 2.0, *)open class UITableView : UIScrollView, NSCoding { }
tableView: UITableView() {} // some another module

2. Public

Like open access level, public access level enable an entity to be used outside the defining module (target). But open access level allows us to subclass it from another module where in public access level, we can only subclass or overridde it from within the module it is defined. Look at the following example.

//module 1public func A(){}
open func B(){}
//module 2override func A(){} // error
override func B(){} // success
  • public classes and class members can only be subclassed and overridden within the defining module (target).
  • open classes and class members can be subclassed and overridden both within and outside the defining module (target).
  • Classes with public access, or any more restrictive access level, can be subclassed only within the module where they’re defined.
  • Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined.
  • Open classes can be subclassed within the module where they’re defined, and within any module that imports the module where they’re defined.
  • Open class members can be overridden by subclasses within the module where they’re defined, and within any module that imports the module where they’re defined.

3. internal (default access level)

internal is the default access level. Internal classes and members can be accessed anywhere within the same module(target) they are defined. You typically use internalaccess when defining an app’s or a framework’s internal structure.

internal func internalMethod() {
print("I am inside UIKit")
}

4. fileprivate

Restricts the use of an entity to its defining source file. You typically use fileprivate access to hide the implementation details of a specific piece of functionality when those details are used within an entire file. ie; the functionality defined with a fileprivate access level can only be accessed from within the swift file where it is defined.

// A.swiftfileprivate func someFunction() {
print("I will only be called from inside A.swift file")
}
// viewcontroller.swiftoverride func viewDidLoad() {
super.viewDidLoad()
let obj = A()
A.someFunction() // error
}

5. private — (most restrictive)

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. You typically use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

// A.swiftclass A {
private var name = "First Letter"
}
extension A {
func printName(){
print(name) // you may access it here from swift 4. Swift 3 will throw error.
}
}
A()
A().name // Error even if accessed from outside the class A{} of A.swift file.

Important: Before swift 4, private access level didn’t allow the use of a class member inside the extension of same class.

Important with private access level for swift 3:

If you are familiar with extensions, you might have already fallen in love with it. Extension is the way by which we can add functionality to a class, enum or struct. Suppose if we have a class called A and we have to add a new functionality to it, use an extension as shown below.

extension A {
// new functionality can be added here
}
class RootViewController: UIViewController {
private var someFlag = false
}
extension RootViewController: someDelegate {
func doSomething {
if someFlag { //error :Use of unresolved identifier 'someFlag'
// do the thing
}
}
}
class RootViewController: UIViewController {
fileprivate var someFlag = false
}

Interview questions:

  • What is the difference between public and open?
  • An open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.
  • A public class is accessible but not subclassable outside of the defining module. A publicclass member is accessible but not overridable outside of the defining module.
  • What is the default access level in swift?

If you enjoyed reading this post, please share and recommend it so others can find it 👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻 !!!!

--

--

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
Abhimuralidharan

Abhimuralidharan

iOS and tvOS developer, dreamer, photographer 🤨