Browse Questions
- 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.
- Timer and CADisplayLink cause memory leaks because the target-action API causes the active RunLoop to retain the timer, and the timer retains its target object until explicitly invalidated.
- Inject mock dependencies, call ViewModel methods, and assert on @Published properties.
- Use Fastlane's firebaseappdistribution plugin to build an Ad Hoc .ipa and upload it to Firebase — testers receive a download link by email within minutes.
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()