Search code examples
iosswiftsolid-principles

Liskov Substitution Principle on nibless UIView subclass


I have a subclass of UIView which is not loadable through nib/storyboard. Does this break the SOLID? Specially Liskov Substitution Principle?

import UIKit

open class NiblessView: UIView {
    
    // MARK: Public Methods
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    @available(*, unavailable, message: "Loading this view from a nib is unsupported in favor of initializer dependency injection.")
    public required init?(coder: NSCoder) {
        fatalError("Loading this view from a nib is unsupported in favor of initializer dependency injection.")
    }
}

Solution

  • It does.

    The meta-class that represents NiblessView can no longer be used instead of the UIView meta-class. For example, this would crash:

    func foo<C: UIView>(_ class: C.Type) {
      class.init(coder: aCoder)
    }
    
    foo(NiblessView.self)