Swinject: Cannot call value of non-function type 'AssemblyType.Type'

Created on 21 Nov 2016  路  1Comment  路  Source: Swinject/Swinject

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?

question

Most helpful comment

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 馃槈

>All comments

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 馃槈

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mijail picture Mijail  路  6Comments

movses-margaryan picture movses-margaryan  路  5Comments

alynmuntean picture alynmuntean  路  6Comments

drekka picture drekka  路  7Comments

grzegorzkrukowski picture grzegorzkrukowski  路  7Comments