Swift Diary #1: Class or Struct from String

Like Mike Ash and his Friday Q&A blog posts, I take requests from readers. Unlike Mike Ash, all I have is questions.

This one comes from my co-worker Jim Correia, and is related to a question I posted on Twitter last night about instantiating an object whose class is known at runtime rather than compile time.

For reference, here’s the answer to last night’s question.

Jim takes this one step further: what if all you have is the class name as a string?

This isn’t an academic question — it’s something app developers do. For example, do a Show Package Contents on any Omni app and look inside Info.plist — you will likely find class names in the plist. (Example: see OFControllerClass in OmniFocus’s plist. Also look inside the OIInspectors array.)

This is a useful pattern when you have a general framework of some kind and you want to configure it for a specific app. The framework doesn’t know which specific classes to use, so you tell it.

And then, in the code, at some point it comes down to doing an NSClassFromString(s) to get a class, and then instantiating an object.

I want to do the same thing in Swift, but with additional criteria:

  • I don’t want to have to mark the class an Objective-C class. I want to use pure Swift.

  • I want to be able to use structs interchangeably with classes. Whether the thing is a class or a struct is a detail that’s private to the class or struct, and not something the general framework needs to know.

I realize that this may seem marvelously unsafe to some people. But, in practice, it’s not. And it’s super-common. (And it’s not terribly different from working with Interface Builder, where you configure by typing in class names.)

I’ll update this post with the answer. (I assume there is one.)

* * *

Update 10:05 am: @jckarter writes:

Possible, but not implemented yet.

20 Jul 2015

Archive