使用添加默认参数的方法扩展协议

Extension of protocol with method which adds default parameter

我习惯在使用扩展的协议中使用默认参数,因为协议声明本身不能有它们,就像这样:

protocol Controller {
   func fetch(forPredicate predicate: NSPredicate?)
}

extension Controller {
   func fetch(forPredicate predicate: NSPredicate? = nil) {
      return fetch(forPredicate: nil)
   }
}

非常适合我。

现在我有下一种情况,我有一个针对特定类型控制器的特定协议:

protocol SomeSpecificDatabaseControllerProtocol {
    //...
    func count(forPredicate predicate: NSPredicate?) -> Int
}

以及带有控制器默认方法实现的协议扩展:

protocol DatabaseControllerProtocol {
    associatedtype Entity: NSManagedObject
    func defaultFetchRequest() -> NSFetchRequest<Entity>
    var context: NSManagedObjectContext { get }
}

extension DatabaseControllerProtocol {
    func save() {
        ...
    }

    func get() -> [Entity] {
        ...
    }

    func count(forPredicate predicate: NSPredicate?) -> Int {
        ...
    }

    //.....
}

我的问题是,当我尝试将带有默认参数的方法添加到 SomeSpecificDatabaseControllerProtocol 扩展时,我收到一个编译时错误,具体 class 符合 SomeSpecificDatabaseControllerProtocol 不符合协议(仅当我扩展协议时才会发生):

class SomeClassDatabaseController: SomeSpecificDatabaseControllerProtocol, DatabaseControllerProtocol {...}

我错过了什么?

发生这种情况是因为编译器因函数不明确而混淆。

  1. Here SomeClassDatabaseController receiving count() method from two different protocols.

  2. DatabaseControllerProtocol has count(forPredicate) method which always need parameter.

  3. On other hand SomeSpecificDatabaseControllerProtocol have count() method which can have empty parameter.

  4. To solve this either you have to change count method in DatabaseControllerProtocol to this or you have to implement it in SomeClassDatabaseController.

func count(forPredicate predicate: NSPredicate? = nil) -> Int { return 0}