What are Swift access control levels?
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.