What are Swift access control levels?
Why Interviewers Ask This
This is a classic screening question for Swift & iOS roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
Swift has five access control levels that control the visibility of entities (classes, methods, properties) across modules and files: (1) open: most permissive — accessible from any module AND can be subclassed/overridden externally. Only for classes and members: open class BaseViewController: UIViewController { open func setup() {} // Can be overridden in other frameworks }; (2) public: accessible from any module but cannot be subclassed/overridden externally: public struct APIClient { public init() {} public func fetch() {} }; (3) internal: (DEFAULT) accessible within the same module (app or framework). No explicit keyword needed; (4) fileprivate: accessible only within the same source file: fileprivate class HelperClass {} // Only used in this file; (5) private: most restrictive — accessible only within the enclosing declaration (class, struct, extension in same file): class BankAccount { private var balance: Double = 0 // Only BankAccount can touch this private func validate() -> Bool { balance >= 0 } public func deposit(_ amount: Double) { guard amount > 0 else { return } balance += amount } }. Best practices: start with private, relax as needed; properties should usually be private or fileprivate; use internal for types within your app; use public/open when building frameworks. Extensions: extensions in same file can access private members. Extensions in different files can only access fileprivate and above. Getter/setter different access: private(set) var count = 0 // Public read, private write.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Swift & iOS candidates.