Access control in Swift: private, fileprivate, internal, package, public and open

Swift’s access control decides which code can see a declaration: the same type, the same file, the same module, the same package, or anyone who imports the module. It’s how a framework hides its implementation and exposes a small public API, and inside an app it stops one file from reaching into another type’s internals.
The six levels
Section titled “The six levels”From most to least restrictive:
| Level | Visible to |
|---|---|
private | The enclosing declaration, plus extensions of it in the same file |
fileprivate | The whole source file |
internal (default) | The whole module |
package | Every module in the same Swift package (Swift 5.9+) |
public | Any module that imports it, but it can’t be subclassed or overridden outside the module |
open | Any module that imports it, and classes and members can be subclassed and overridden there |
A “module” is one build target: an app, a framework, or a SwiftPM target.
private
Section titled “private”private limits access to the enclosing type and its extensions in the same file. Extensions have been included since Swift 4. Before that, private meant the declaration’s scope only.
class Counter { private var count = 0
func increment() { count += 1 // OK: inside Counter }}
extension Counter { func reset() { count = 0 // OK: extension of Counter in the same file }}
let counter = Counter()counter.increment()// counter.count // error: 'count' is inaccessible due to 'private' protection levelA common variant is private(set). It makes the setter private but leaves the getter at the declaration’s level:
class Counter { private(set) var count = 0 // anyone can read count; only Counter can change it}fileprivate
Section titled “fileprivate”fileprivate opens access to everything in the same source file, including other types:
class Engine { fileprivate var temperature = 90}
class Dashboard { func showTemperature(of engine: Engine) { print(engine.temperature) // OK: same file }}Use it when two closely related types in one file need to share details. If you reach for it often, the types may belong together.
internal
Section titled “internal”internal is the default. If you don’t write an access modifier, the declaration is visible throughout its module and invisible outside it. For app code, that’s usually all you need.
// Module: Networkingpublic class APIClient { public init() {}
var retryCount = 3 // internal by default
public func fetch() { /* ... */ }}// Module: Appimport Networking
let client = APIClient()client.fetch() // OK: public// client.retryCount // error: 'retryCount' is inaccessible due to 'internal' protection levelNote that the class needs a public init(). A type’s default initialiser is internal, so without it the App module could see APIClient but not create one.
package
Section titled “package”package, added in Swift 5.9 (SE-0386), sits between internal and public. It makes a declaration visible to other targets in the same Swift package but not to clients of the package. Before it existed, sharing helpers between targets meant making them public and hoping nobody used them.
// Target: Core (in package MyLibrary)import Foundation
package func makeRequestID() -> String { UUID().uuidString }// Target: Networking (same package)import Core
let id = makeRequestID() // OK: same packageSwiftPM passes the package name to the compiler automatically. If you build outside SwiftPM, you set it with the -package-name flag.
public
Section titled “public”public makes a declaration usable from other modules. For classes, it doesn’t allow subclassing or overriding outside the defining module:
// Module: UIKitLitepublic class Button { public init() {} public func tap() { print("tapped") }}// Module: Appimport UIKitLite
let button = Button()button.tap() // OK
// class FancyButton: Button {}// error: cannot inherit from non-open class 'Button' outside of its defining moduleopen applies only to classes and overridable class members. It’s public plus permission to subclass and override from other modules. Each member must opt in separately:
// Module: UIKitLiteopen class View { public init() {}
open func draw() { print("View.draw") } // can be overridden anywhere public func layout() { print("layout") } // callable anywhere, overridable only here}// Module: Appimport UIKitLite
class ChartView: View { override func draw() { print("ChartView.draw") } // OK: draw() is open // override func layout() {} // error: overriding non-open instance method outside of its defining module}
ChartView().draw() // ChartView.drawRules that trip people up
Section titled “Rules that trip people up”- Members of a public type are internal by default. Marking a type
publicdoesn’t make its properties, methods or initialiser public; you mark each one. - Nothing can be more visible than the types it uses. A
publicfunction can’t take or return aninternaltype, and apublicproperty can’t have aprivatetype. The compiler reports these errors. - Overrides can widen access. A subclass may make an overridden member more accessible than the superclass version.
- Tests:
@testable import MyModulelets a test target seeinternaldeclarations, as long as the module was built with testing enabled (Xcode and SwiftPM do this for debug builds by default). - Imports have access levels too. Since Swift 6.0 (SE-0409) you can write
internal import Fooso a dependency doesn’t leak into your module’s public interface.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)