Using Swift protocols with generics
问题 I have a simple example that seems like it should work: import CoreData @objc protocol CoreDataModel { @optional class func entityName() -> String } class AbstractModel: NSManagedObject, CoreDataModel { class func create<T : CoreDataModel>(context:NSManagedObjectContext) -> T { var name = T.entityName?() var object = NSEntityDescription.insertNewObjectForEntityForName(name, inManagedObjectContext: context) as T return object } } So we have a class called AbstractModel which conforms to the