1

I have an app I am making in Swift that references a bunch of different classes that are all very similar but slightly different. They all have the same methods and up until now I have been just been instantiating one of them for testing purposes. Now I need to be able to tell the view controller to instantiate and call the methods from a different class. I will not know which class to call until runtime.

var studio = AudioStudio()

Is how I am doing it now. But I want 'studio' to be able to be different versions of AudioStudio. I tried NSClassFromString but that doesn't work. I do want to call the class from the name in a string.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
MichaMusic
  • 23
  • 4
  • *" I tried NSClassFromString but that doesn't work."* – What exactly did you try? Did you see http://stackoverflow.com/a/24570847/1187415? – Martin R Feb 28 '15 at 20:12
  • I tried that and the compiler forces me to type cast it as "AnyClass" so it looks like this . "var studio: AnyClass! = NSClassFromString("AudioToy.AudioStudio")" but then all of the functions I call on "studio" suddenly say 'AnyClass' does not have a member named 'functionName' – MichaMusic Mar 01 '15 at 00:59

0 Answers0