I'm trying to assemble an class to my container form a string but can't get it to work:
Cannot call value of non-function type 'AssemblyType.Type'
It gives the error here: applicationConfigurationAssembly()
let applicationConfigurationAssembly = NSClassFromString(ns + "." + applicationConfigurationAssemblyString) as? AssemblyType.Type{
applicationConfigurationAssembly().assemble(container: SwinjectStoryboard.defaultContainer)
If I remove the () it gives me this error:
Instance member 'assemble' cannot be used on type AssembleType
How is this possible?
hi @laurenswuyts
applicationConfigurationAssembly() is interpreted as calling a function. If you want to call an initialiser you would write it as
applicationConfigurationAssembly?.init() //...
However AssemblyType is a protocol and it does not require initialiser, so code above would also fail. Moreover, creating class from string is not very swift-y. If you need to select your assembly based on external configuration, I would suggest something like:
func makeAssembly(for config: String /* loaded from external source */) -> AssemblyType {
switch config {
case "CaseA":
return MyAssemblyA()
case "CaseB":
return MyAssemblyB()
default:
fatalError()
}
}
Hope this helps 馃槈
Most helpful comment
hi @laurenswuyts
applicationConfigurationAssembly()is interpreted as calling a function. If you want to call an initialiser you would write it asHowever
AssemblyTypeis a protocol and it does not require initialiser, so code above would also fail. Moreover, creating class from string is not very swift-y. If you need to select your assembly based on external configuration, I would suggest something like:Hope this helps 馃槈