I use the default parameters inside protocols using extensions, since the protocol declarations themselves cannot be like this:
protocol Controller { func fetch(forPredicate predicate: NSPredicate?) } extension Controller { func fetch(forPredicate predicate: NSPredicate? = nil) { return fetch(forPredicate: nil) } }
Worked great for me.
Now I have the following situation: I have one specific protocol for a certain type of controller:
protocol SomeSpecificDatabaseControllerProtocol {
And the extension of the protocol with the implementation of standard 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 problem is that when I try to add a method with a default parameter to the SomeSpecificDatabaseControllerProtocol extension, I get a compile-time error that the specific class corresponding to SomeSpecificDatabaseControllerProtocol does not match the protocol (only happens if I extend the protocol):
class SomeClassDatabaseController: SomeSpecificDatabaseControllerProtocol, DatabaseControllerProtocol {...}
What am I missing?
source share