I've got a NSManagedObject subclass Shop
for which I have defined the following function in swift:
// insertions
public class func insertOrUpdateRepresentation(representation: Dictionary<String, AnyObject>, context: NSManagedObjectContext) -> Shop {
let identifier = representation["id"] as! NSNumber
let string = identifier.stringValue
var shop = Shop.fetchObjectWithIdentifier(string, context: context) as! Shop?
// insert if needed
if (shop == nil) {
shop = NSEntityDescription.insertNewObjectForEntityForName(Shop.entityName(), inManagedObjectContext: context) as? Shop
}
// update
shop?.deserializeRepresentation(representation)
return shop!
}
Now, I'd like to define a base class for this object (and others) where I can use the class type in that method. Now in Objective C, I could reference 'self' here to get the current caller's class.
// insertions
public class func insertOrUpdateRepresentation(representation: Dictionary<String, AnyObject>, context: NSManagedObjectContext) -> MyManagedObject {
let identifier = representation["id"] as! NSNumber
let string = identifier.stringValue
var obj = (class of the caller).fetchObjectWithIdentifier(string, context: context) as! (class of the caller)?
// insert if needed
if (obj == nil) {
obj = NSEntityDescription.insertNewObjectForEntityForName((class of the caller).entityName(), inManagedObjectContext: context) as? (class of the caller)
}
// update
obj?.deserializeRepresentation(representation)
return obj!
}
How do I implement this kind of functionality in swift?
Thanks!