I am about to learn Swift. I want to write a class, that handles Cora Data tasks for me. In this case, it should be possible to pass an entity name and and array of tuples to a function, which then interprets the tuples as key value pairs for NSPredicates to retrieve ManagedObjects matching them. Because of NSPredicate needing values with specific types, I tried to pass the types of the values, too. But I can't get around that "'t' is not a type" compile error (last line of code). How can I cast the value to an passed type? Or would you create the NSPredicate in a totally different way?
class func getManagedObjectsWithEntityName<T>(entityName: String, keysAndValues: [(key: String, value: AnyObject, type: T.Type)]) -> [NSManagedObject] {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: entityName)
var predicates: [NSPredicate] = []
for tuple in keysAndValues {
let t = tuple.type
predicates.append( NSPredicate(format: tuple.key + " = %@", tuple.value as t)!)
}
...
}
Aucun commentaire:
Enregistrer un commentaire