2016-11-03 100 views
1
protocol BasePresenterProtocol : class {} 
protocol DashboardPresenterProtocol : BasePresenterProtocol {} 

final class DashboardPresenter { 
    weak var view: DashboardPresenterProtocol? 

    init() { 
     self.view = DashboardViewController() 
    } 

    func test() { 
     print("Hello") 
    } 
} 

extension DashboardPresenter: DashboardViewProtocol { } 

protocol BaseViewProtocol : class { 
    weak var view: BasePresenterProtocol? { get set } 
} 

protocol DashboardViewProtocol : BaseViewProtocol { 
} 

class DashboardViewController { 
} 

extension DashboardViewController: DashboardPresenterProtocol { } 

在上面的代码中,我得到一个错误,在下面的行斯威夫特协议继承和协议一致性问题

extension DashboardPresenter: DashboardViewProtocol { } 

的是,DashboardPresenter没有确认到协议DashboardViewProtocol,但我已经在DashboardPresenter宣布weak var view: DashboardPresenterProtocol? 。虽然我宣称

为什么我得到这个错误?请让我知道我在这段代码中做错了什么。

回答

5

您无法实施类型为BasePresenterProtocol?的类型为DashboardPresenterProtocol?的属性的读写属性要求。

想想看会发生什么,如果这可能,并且您将DashboardPresenter的实例上传到DashboardViewProtocol。您将能够将符合BasePresenterProtocol的任何内容分配给DashboardPresenterProtocol?类型的属性 - 这将是非法的。

为此,是不变的读写性能要求(虽然这是值得注意的,可读的,只有性能要求应该能够为协变 - but this currently isn't supported)。

虽然在任何情况下,protocols don't conform to themselves,所以您甚至不能使用DashboardPresenterProtocol?作为符合BasePresenterProtocol?的类型。

+0

感谢Hamish! – Soni

+0

高兴帮助@Soni :) – Hamish