Browse Questions
- Every UIView has a CALayer that does the actual drawing and compositing.
- All three transform collections but handle the results differently.
- Use separate Xcode Schemes, Build Configurations, Bundle Identifiers, and Firebase plists — one set per environment.
- Encapsulation, abstraction, inheritance, and polymorphism.
Answer: Encapsulation, abstraction, inheritance, and polymorphism. Swift supports all four, though it leans on protocols and value types more than classic class-based OOP languages.
| Pillar | What it means | Swift example |
|---|---|---|
| Encapsulation | Hide internal state, expose controlled access | private(set) var balance |
| Abstraction | Expose what something does, not how | protocol PaymentProcessor |
| Inheritance | Reuse/extend behavior from a base type | class SavingsAccount: Account |
| Polymorphism | Same interface, different underlying behavior | [PaymentProcessor] holding multiple conforming types |
protocol PaymentProcessor {
func charge(_ amount: Decimal) -> Bool
}
class CreditCardProcessor: PaymentProcessor {
func charge(_ amount: Decimal) -> Bool { /* ... */ true }
}
class PayPalProcessor: PaymentProcessor {
func charge(_ amount: Decimal) -> Bool { /* ... */ true }
}
let processors: [PaymentProcessor] = [CreditCardProcessor(), PayPalProcessor()]
processors.forEach { _ = $0.charge(9.99) } // polymorphic dispatch