Last active
June 13, 2016 14:20
-
-
Save balazsnemeth/756fa0b6ca9823bcbff0241a66f0b1ca to your computer and use it in GitHub Desktop.
Trick, how to add a generic function available for each NSManagedObject subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension NSObjectProtocol where Self: NSManagedObject { | |
static func findAllWithPredicate(searchTerm: NSPredicate?, inContext context: NSManagedObjectContext = NSManagedObjectContext.MR_defaultContext()) -> [Self] { | |
//You can implement the fetchrequest here by yourself, but now I'm using MagicalRecord service to keep it simple | |
return self.MR_findAllWithPredicate(searchTerm, inContext: context) as! [Self] | |
} | |
} | |
Usage (There is no cast required here, but the result is a [AnswerObject]): | |
let unsynchedObjects = AnswerObject.findAllWithPredicate(NSPredicate(format: "synchronized == false")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment