Skip to content

Instantly share code, notes, and snippets.

@nil-biribiri
Created February 21, 2019 09:52
Show Gist options
  • Save nil-biribiri/9a7a3e8343e7ee73102b9a27c5c2347d to your computer and use it in GitHub Desktop.
Save nil-biribiri/9a7a3e8343e7ee73102b9a27c5c2347d to your computer and use it in GitHub Desktop.
Custom view protocols for CustomView in UIViewController
import UIKit
public protocol HasCustomView {
associatedtype CustomView: UIView
}
extension HasCustomView where Self: UIViewController {
/// The UIViewController's custom view.
public var customView: CustomView {
guard let customView = view as? CustomView else {
fatalError("Expected view to be of type \(CustomView.self) but got \(type(of: view)) instead")
}
return customView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment