I'm used to having default parameters inside protocols using extensions, as protocol declarations themselves can't have them, like this:
protocol Controller {
func fetch(forPredicate predicate: NSPredicate?)
}
extension Controller {
func fetch(forPredicate predicate: NSPredicate? = nil) {
return fetch(forPredicate: nil)
}
}
Worked perfectly for me.
Now I have the next situation, I have one specific protocol for specific kind of controller:
protocol SomeSpecificDatabaseControllerProtocol {
//...
func count(forPredicate predicate: NSPredicate?) -> Int
}
And protocol-extension with implementations of default methods for controllers:
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 {
...
}
//.....
}
My issue is when I'm trying to add method with default parameter to the SomeSpecificDatabaseControllerProtocol
extension, I'm receiving a compile-time error, that concrete class conforming to SomeSpecificDatabaseControllerProtocol
doesn't conform to the protocol (happens only if I extend protocol):
class SomeClassDatabaseController: SomeSpecificDatabaseControllerProtocol, DatabaseControllerProtocol {...}
What am I missing?