vendredi 9 juin 2017

Instantiating classes stored in metatype Dictionary

I've followed the solution at Make a Swift dictionary where the key is "Type"? to create dictionaries that can use a class type as keys. What I want to do is: I have one dictionary that should store class types with their class type (aka metatype) as keys, too:

class MyScenario {
    static var metatype:Metatype<MyScenario> {
        return Metatype(self)
    }
}


var scenarioClasses:[Metatype<MyScenario>: MyScenario.Type] = [:]

Then I have methods to register and execute scenarios:

public func registerScenario(scenarioID:MyScenario.Type) {
    if (scenarioClasses[scenarioID.metatype] == nil) {
        scenarioClasses[scenarioID.metatype] = scenarioID
    }
}

public func executeScenario(scenarioID:MyScenario.Type) {
    if let scenarioClass = scenarioClasses[scenarioID.metatype] {
        let scenario = scenarioClass()
    }
}

... Problem is in the last line:

Constructing an object of class type 'MyScenario' with a metatype value must use a 'required' initializer.

It looks like the compiler is confused at that point since I cannot use 'required' at that assignment. Does anyone have an idea how I would have to instantiate the scenarioClass in executeScenario()?





Aucun commentaire:

Enregistrer un commentaire