wnagrodzki / iOSProgrammingGuidelines

2 stars 0 forks source link

Protocol extension should not provide undeclared method implementations #31

Open wnagrodzki opened 5 years ago

wnagrodzki commented 5 years ago

It is usually not obvious that static dispatch is going to be used. If object implementing MyProtocol delivers it own numberOfItems implementation it is not going to be called. (add an example to show the problem)

protocol MyProtocol {
    func item(at index: Int) -> String
}

extension MyProtocol {
    var numberOfItems: Int { return 4 }
}

Declare methods added via protocol extension.

protocol MyProtocol {
    var numberOfItems: Int { get }
    func item(at index: Int) -> String
}

extension MyProtocol {
    var numberOfItems: Int { return 4 }
}