Swift Protocol inheritance and protocol conformance issue

You cannot implement a read-write property requirement of type BasePresenterProtocol? with a property of type DashboardPresenterProtocol?. Consider what would happen if this were possible, and you upcast an instance of DashboardPresenter to DashboardViewProtocol. You would be able to assign anything that conforms to BasePresenterProtocol to a property of type DashboardPresenterProtocol? – which would be illegal. … Read more

A Swift protocol requirement that can only be satisfied by using a final class

The Error is correct. You have to make your class final, since no subclasses could conform your protocol Ownee. Consider this subclass: class FirstGradeStudent: Student { // Student contains following variable: // var owner: Owner<Student> { // return professor // } } As you can see, it would have to implement var owner: Owner<Student> because … Read more

How does one declare optional methods in a Swift protocol?

1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform … Read more

How to define optional methods in Swift protocol?

1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform … Read more

Non-‘@objc’ method does not satisfy optional requirement of ‘@objc’ protocol

While I think I can answer your question, it’s not an answer you will like. TL;DR: @objc functions may not currently be in protocol extensions. You could create a base class instead, though that’s not an ideal solution. Protocol Extensions and Objective-C First, this question/answer (Can Swift Method Defined on Extensions on Protocols Accessed in … Read more

What is the in-practice difference between generic and protocol-typed function parameters?

(I realise that OP is asking less about the language implications and more about what the compiler does – but I feel it’s also worthwhile also to list the general differences between generic and protocol-typed function parameters) 1. A generic placeholder constrained by a protocol must be satisfied with a concrete type This is a … Read more