传递协议功能的零值(SWIFT)
我想在协议的函数中传递零值,但协议会出现错误:“默认参数不允许在协议方法中”。
protocol CoreDataRepositoryProtocol: ExpressibleByNilLiteral{
func fetchData<T:NSManagedObject>(entity: T.Type, enityName: entittytype, predicate: NSPredicate? = nil) -> [T]?
func getOne <T:NSManagedObject>(entity: T.Type, enityName: String, predicate: NSPredicate) -> T?
func deleteRec<T:NSManagedObject>(entity: T.Type, entityName:entittytype, predicate: NSPredicate, completion: @escaping (String) -> Void) -> Bool
func saveObject()
}
第一个函数,我想添加谓词为predicate:nspredicate:nspredicate?= nil
nil 但是协议不接受
I wanted to pass nil value in a function of protocol but protocol gives error: "Default argument not permitted in a protocol method"
protocol CoreDataRepositoryProtocol: ExpressibleByNilLiteral{
func fetchData<T:NSManagedObject>(entity: T.Type, enityName: entittytype, predicate: NSPredicate? = nil) -> [T]?
func getOne <T:NSManagedObject>(entity: T.Type, enityName: String, predicate: NSPredicate) -> T?
func deleteRec<T:NSManagedObject>(entity: T.Type, entityName:entittytype, predicate: NSPredicate, completion: @escaping (String) -> Void) -> Bool
func saveObject()
}
The first function , I want to add predicate as predicate:NSPredicate?=nil
but protocol is not accepting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误所说,您无法在协议中提供默认值。
您的协议可以声明
您可以在编写符合此协议的对象时提供
nil
的默认值As the error says, you can't provide a default value in the protocol.
Your protocol can declare
You can provide a default value of
nil
when you write implement an object that conforms to this protocol