I try to init a class in a generic way with parameters but it doesn't work...
protocol EntityCreator: class {
func createEntity<EntityType: EntityClass & EntityCreatorInitializable>(type: EntityType.Type, _ initialize: @escaping (EntityType) -> Void)
}
protocol EntityCreatorInitializable {
init(entityCreator: EntityCreator)
}
class EntityClass: NSObject { }
class MyClass: EntityClass, EntityCreatorInitializable {
required init(entityCreator: EntityCreator) {
super.init()
// use the entityCreator
}
}
// On the entity creator implementation :
class EntityCreatorImplementation: EntityCreator {
var toAdd = Set<EntityClass>()
func createEntity<EntityType: EntityClass & EntityCreatorInitializable>(type: EntityType.Type, _ initialize: @escaping (EntityType) -> Void) {
// This creation doesn't work...
let newEntity = EntityType(entityCreator: self)
initialize(newEntity)
self.toAdd.insert(newEntity)
}
}
The error I have at compilation is
Non-nominal type 'EntityType' does not support explicit initialization
Any Ideas on how to achieve this ?
Thanks !!
EDIT : I added the definition of EntityClass. In my project it is a GKEntity, but the problem is the same with just NSObject
EDIT : added the toAdd Set, forgot to declare it (but used it in the createEntity method)