Browse Questions
  • A Singleton ensures only one instance of a class exists globally.
  • SOLID is a set of five design guidelines for maintainable OOP code: | Principle | Idea | Swift application | |--|--|--| | Single Responsibility | A type should have one reason to change | Split a…
  • A retain cycle occurs when two objects hold strong references to each other, preventing ARC from deallocating either.
  • Yes — passing an observed property as inout always calls both willSet and didSet, even if the function never modifies the value.

Answer: SOLID is a set of five design guidelines for maintainable OOP code:

PrincipleIdeaSwift application
Single ResponsibilityA type should have one reason to changeSplit a "God ViewController" into a ViewModel + Service
Open/ClosedOpen for extension, closed for modificationAdd behavior via protocol conformance/extensions, not by editing existing types
Liskov SubstitutionSubtypes must be usable wherever their base type is expectedA Square: Rectangle shouldn't break callers that resize rectangles independently
Interface SegregationPrefer many small protocols over one large oneCodable is Encodable & Decodable — adopt only what you need
Dependency InversionDepend on abstractions, not concrete typesInject a NetworkClientProtocol, not a concrete URLSessionClient
// Interface Segregation + Dependency Inversion together
protocol UserFetching {
    func fetchUser(id: String) async throws -> User
}

final class ProfileViewModel {
    private let fetcher: UserFetching   // depends on an abstraction

    init(fetcher: UserFetching) {
        self.fetcher = fetcher
    }
}