This is what I need to achieve:
extension NSManagedObject {
class func all<T: Self>(in context: NSManagedObjectContext) -> [T] { //error: Expected a class type or protocol-constrained type restricting '
return mr_findAll(in: context) as! [T]
}
}
And I would like to use it like this:
let persons = Person.all(in: context)
let hotels = Hotel.all(in: context)
Is it possible at all?
Person and Hotel inherits from NSManagedObject.
As a fetch request is already generic this can be accomplished only with a protocol extension and associated type, an
NSManagedObjectextension doesn't work without any type cast or annotation.Just add conformance to
Managedto all yourNSManagedObjectsubclasses then you can use