I bit of help with Swift construction, chapter 24

Hi

I have a question about this code snippet from chapter 24.

extension BindableType where Self: UIViewController {
    mutating func bindViewModel(to model: Self.ViewModelType) {
    viewModel = model
    loadViewIfNeeded()
    bindViewModel()
} }

what does this line mean?

extension BindableType where Self: UIViewController

especially Self:UIViewController

Thnx

It’s a protocol extension on BindableType which is restricted only to objects also implementing UIViewController, you can check the Swift docs on protocols and extensions.

Also here’s a code you can paste in a playground to see a simpler example:

protocol Hello {
    func sayHello()
}

protocol Automatic { }

extension Hello where Self: Automatic {
    func sayHello() {
        print("Automatic Hello")
    }
}

struct Test: Hello, Automatic { }

let t = Test()
t.sayHello() --> exists and prints "Automatic Hello"

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.