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扩展时,我收到编译时错误,符合SomeSpecificDatabaseControllerProtocol的具体类不符合协议(仅当我扩展协议时才会发生) :
class SomeClassDatabaseController: SomeSpecificDatabaseControllerProtocol,DatabaseControllerProtocol {...}
我错过了什么?
解决方法
Here
SomeClassDatabaseController
receivingcount()
method from two different protocols.
DatabaseControllerProtocol
hascount(forPredicate)
method which always need parameter.On other hand
SomeSpecificDatabaseControllerProtocol
havecount()
method which can have empty parameter.To solve this either you have to change count method in
DatabaseControllerProtocol
to this or you have to implement it inSomeClassDatabaseController
.
func count(forPredicate predicate: nspredicate? = nil) -> Int { return 0}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。