Swift — Access Control

1. open and public — (least restrictive)

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

2. Public

//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 func internalMethod() {
print("I am inside UIKit")
}

4. fileprivate

// 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)

// 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:

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 👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻👏🏻 !!!!

--

--

--

iOS and tvOS developer, dreamer, photographer 🤨

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Set Up and Use Trust Wallet for Binance Smart Chain (Airdrop tip 1)

Notes on lecture #2 of iOS Developer Crash Course, provided by Essential Developer Academy

An example of code that violates the Liskov Substitution principle

How to Implement Sign In With SwiftUI and AWS Amplify (Part 1)

A macbook with Xcode open

Presale is Announced!

Persistent History Tracking in Core Data

Toy sailboat on a wooden shelf

Capture Memory Leak with XCTest

Memory Leak object graph

App Preview videos for Apple App Store without a device

UICollection Compositional Layout Part 2

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 🤨

More from Medium

Access specifiers in Swift

How to convert Objc NSString “enum” to real Swift enum?

Stretchable Headers in UITableView

How a well-crafted function for Decodable data request should be