In Swift, what access level does a declaration get when you write no modifier, and how do `private` and `fileprivate` differ, including in extensions?
answer
- module-wide unless you say otherwise
- scope versus file
- same-file extensions share one scope
- subclasses are not extensions
basics
~20 sWith no modifier a Swift declaration is internal: usable in every file of its module, invisible outside it. private limits use to the enclosing declaration and its same-file extensions; fileprivate opens it to the whole source file.
solid answer
~40 sThe default is `internal`, so in a single-target app everything is already visible app-wide and nothing leaks outside the module. `private` is scoped to the enclosing declaration, and since Swift 4 a type and all of its extensions **in the same file** count as one scope: a private member of the type is usable in those extensions, and a private member declared in one extension is usable in the type and its other same-file extensions. Nothing private crosses into another file, and it does not reach a subclass or a free function, even one in the same file. `fileprivate` makes a declaration visible to any code in its source file, which is what you need when a helper function, a sibling type or a subclass written beside the type must touch the member.
code
swift · 22 lines// ThumbnailCache.swift, one source file of the Core module
struct ThumbnailCache {
private var entries: [String: Int] = [:] // this type and its extensions in this file
fileprivate var hits = 0 // any code in this file
mutating func store(_ key: String, size: Int) {
entries[key] = size
trim() // private member declared in an extension
}
}
extension ThumbnailCache {
private mutating func trim() {
if entries.count > 100 { entries.removeAll() }
}
var count: Int { entries.count } // no modifier: internal, the whole module
}
func debugSummary(_ cache: ThumbnailCache) -> String {
// A free function in the same file sees `hits` (fileprivate), not `entries` (private).
"hits: \(cache.hits), entries: \(cache.count)"
}go deeper
Recall that no modifier means internal, then state private as the declaration plus its same-file extensions and fileprivate as the whole file.
Explain the single access scope a type shares with its same-file extensions, and name what it excludes: other files, subclasses and free functions.
Show the judgment: default to private, widen to fileprivate only for a named same-file collaborator, and treat a test that needs private state as a missing seam.
Frame the rule a team adopts for narrowing access by default, and how reviews catch fileprivate used out of habit rather than need.
## The six levels and the default Swift's access control is built on three units: the **source file**, the **module** (one build target such as an app, a framework or a test bundle) and the **package** (a group of modules built together). It has six access levels, from widest to narrowest: | Level | Who can use the declaration | |---|---| | `open` | any module that imports it; classes and class members can also be subclassed or overridden there | | `public` | any module that imports it; subclassing and overriding stay inside the defining module | | `package` | any module compiled with the same package name | | `internal` | any source file of the defining module | | `fileprivate` | the defining source file | | `private` | the enclosing declaration, plus extensions of that type in the same file | When you write no modifier, a declaration is **`internal`**. In a single-target app that already means "the whole app", which is why many apps rarely write a modifier except to hide details. A few contexts shift the default: - members of a `private` or `fileprivate` type default to that type's level; - members of a `public` type default to `internal`, not `public`; - enum cases always have their enum's level and cannot be given another; - a protocol's requirements always have the protocol's level. ## `private`: one declaration and its same-file extensions `private` restricts a declaration to the **enclosing declaration**. Since Swift 4 (proposal SE-0169), a type and every extension of it **in the same source file** form a single access scope. That has concrete consequences: - a private stored property of the type is readable and writable in its same-file extensions; - a private method declared in an extension can be called from the type body and from its other same-file extensions; - an extension of the same type in **another file** of the module cannot see it: the compiler reports `'entries' is inaccessible due to 'private' protection level`; - a **subclass** gets no private access, even when it is declared in the same file, because the rule covers extensions of the type itself, not types derived from it; - a free function or a different type in the same file cannot see the private member either. A `private` declaration at the top level of a file has no enclosing type, so its scope is the file: other declarations in that file can use it, other files cannot. ## `fileprivate`: the whole source file `fileprivate` makes a declaration visible to **every declaration in the same source file**: free functions, other types, subclasses and extensions alike. It is the right tool when a collaborator that is neither the type nor its extension must reach a member: a debug helper, a small sibling type that builds the value, or a subclass kept beside its superclass. The attached example is one file of a Core framework: `ThumbnailCache` keeps `entries` private and calls a private `trim()` declared in its own extension, while a free function in the same file can read the `fileprivate` counter `hits` but not `entries`. ## Common mistakes - Expecting a superclass's `private` member to be usable from a subclass, as a protected level would allow; Swift has no protected level, so the member needs `fileprivate` (subclass in the same file) or `internal` (same module). - Splitting a type's extensions into separate files for tidiness, then finding they can no longer touch the type's private storage. - Assuming a modifier on an extension, such as `fileprivate extension`, widens access to members declared elsewhere; it only sets the default for the members that extension declares. ## Choosing a level 1. Start with the narrowest level that compiles: `private` for storage and helpers only the type itself uses. 2. Widen to `fileprivate` only when a same-file collaborator outside the type and its extensions needs the member. 3. Leave the rest `internal`, and widen to `package`, `public` or `open` only when another module must see it. Two signals in code review: `fileprivate` sprinkled on members that only same-file extensions use, a habit left over from before Swift 4 when `private` did not reach extensions; and private state a test cannot reach, which usually asks for an internal seam rather than a wider modifier, since even `@testable import` never exposes `private` or `fileprivate`.
- Can a Swift extension in another file add a `private` member and use it from the type's original declaration?No. The shared scope only covers the type and its extensions in the **same** file. A `private` member declared in an extension in another file is visible to that extension and to other extensions of the type in that file, but the original declaration in its own file cannot see it, and the extension cannot see the original declaration's private members either.
- Does putting a modifier on the extension itself, such as `private extension ThumbnailCache`, change who can see the type's existing private members?No. A modifier on an extension only sets the default access level of the members that extension declares; each member may still override it. It grants nothing extra. An extension that adds a protocol conformance cannot carry an access modifier at all; the requirement implementations take their default from the protocol instead.
- Swift declarations without a modifier are public to every importing module
- private members are visible to every extension of the type in the module
- A subclass in the same file can call its superclass's private methods
- fileprivate and private are the same thing since Swift 4
- A free function in the same file can read a type's private properties