Browse Questions
- some declares an opaque return type — the function returns a specific concrete type that conforms to a protocol, but the caller doesn't need to know which type it is.
- intrinsicContentSize is the natural size a view prefers based on its content — like a label fitting its text, or a button fitting its title.
- Encapsulation means bundling data with the operations that act on it, and restricting direct access to that data so internal invariants can't be broken from outside.
- Xcode File Templates are blueprint files and metadata plists stored in ~/Library/Developer/Xcode/Templates/File Templates/ that allow generating multiple pre-wired Swift files with automatic text…
Answer: Encapsulation means bundling data with the operations that act on it, and restricting direct access to that data so internal invariants can't be broken from outside. Swift enforces this through access control levels rather than getter/setter conventions.
| Level | Visibility |
|---|---|
private | Within the enclosing declaration (and extensions in the same file) |
fileprivate | Anywhere in the same file |
internal (default) | Anywhere in the same module |
public | Any module, but not overridable/subclassable outside |
open | Any module, including override/subclass |
class BankAccount {
private(set) var balance: Decimal = 0 // readable outside, writable only inside
func deposit(_ amount: Decimal) {
guard amount > 0 else { return } // invariant protected here
balance += amount
}
}
let account = BankAccount()
account.deposit(100)
// account.balance = 1_000_000 // ❌ compile error — can't bypass deposit()