open, public, internal, fileprivate and private in one table, with a notebook.
In one line
Deciding how far types and members are visible. Swift has five levels, reaching from inside the type, to the file, to the module (one app or package target), to other modules.
Why you need it
If the notebook’s page array is open, screen code skips the lock check and appends directly. In a library, once users rely on the internal array you can never change its shape.
Make only what outsiders need public, leave app-wide code at the default internal, and keep helpers private to a file or type. Every change then touches a smaller area.
Everyday picture
Think of an apartment complex. The drawer in your room (private) is yours alone, the living room (fileprivate) is for the family, the playground (internal) for residents. The shopping arcade (public) is open to anyone, and some of its shops can be leased and refitted by others (open).
Swift code does not run on this page. Save your edited code as main.swift on your own computer, compile it with swiftc and run it.
Where each level is visible (module = one target of an app or package)
Access level
Same type
Same file
Same module
Use from another module
Subclass or override from another module
open
O
O
O
O
O
public
O
O
O
O
X
internal
O
O
O
X
X
fileprivate
O
O
X
X
X
private
O
X
X
X
X
With nothing written, the level is internal. private members are also visible in extensions of the same type in the same file. Since Swift 5.9 there is also package, open only to modules in the same package.
When to use it
API a library shows the world: public; open only for classes meant to be subclassed outside
Most app code is fine with the default internal
State inside a type: private; helpers shared within one file: fileprivate
Open reading but close writing with pairs such as public private(set)
Inside a single app, public and internal look almost the same. The difference appears once code is split into a library that another module imports.
Members of a public type need their own public to be visible outside. Open the type alone and outsiders see an empty box.
Common mistakes
Reading a private property from outside
private is visible only inside the type (and its extensions in the same file). If outsiders need a value, open a separate read-only door such as pageCount.
Wrong
let note = Notebook(owner: "Kai")print(note.pages.count)
What the Swift compiler sayserror: 'pages' is inaccessible due to 'private' protection level
Fixed
let note = Notebook(owner: "Kai")print(note.pageCount)
When you run it0
A public method that takes an internal type
Outsiders could call pin but never create a Sticker, so the compiler refuses. Open the parameter type to the same level. A public struct does not get a public memberwise init, so write one.
What the Swift compiler sayserror: method cannot be declared public because its parameter uses an internal type
Fixed
let board = Board()board.pin(Sticker(text: "hi"))print(board.count)publicstructSticker {publiclet text: Stringpublicinit(text: String) {self.text = text }}publicfinalclassBoard {privatevar stickers: [Sticker] = []publicvar count: Int { stickers.count }publicfuncpin(_ sticker: Sticker) { stickers.append(sticker) }}
When you run it1
Marking a struct open
open means "other modules may subclass and override", so it applies only to classes and their members. Structs cannot be inherited, so public is as open as they get.
What access level does a method get when none is written?
Show the answer
Answer: 2. internalinternal: visible anywhere in the same module (one app, or one package target), and hidden from other modules.
Question 2
A write is tried after locking. What is the last line?
let note = Notebook(owner: "Ko")note.lock(pin: "1")note.write("hi")print(note.pageCount)
Show the answer
Answer: 1. 0 A locked notebook is stopped by the guard in write. The count stays 0, and the refusal line prints first.
Questions people ask
What does no modifier mean?
internal: visible anywhere in the same module, meaning one app or one package target, and hidden from other modules.
How do open and public differ?
Both are usable from other modules. open also lets other modules subclass and override; public allows use only. open applies only to classes and their members.
How do private and fileprivate differ?
private is visible only inside the type (and its extensions in the same file). fileprivate is visible to other types and functions in the same file.