baileyseymour baileyseymour - 7 months ago 13
Swift Question

Swift Objective-C runtime class naming

I've noticed that a Swift Class is renamed in objective-c runtime. So if I had a class in swift named

ViewController
and the name of my app was
TestRuntime
when I perform
object_getClass(self)
, self being
ViewController
, I would get this:
_TtC11TestRuntime14ViewController
. I've noticed this pattern or format:
_TtC$$AppName$$ClassName
, $ being a random number. I can't figure out where these numbers are coming from. If someone could shed some light on how swift names its classes in the runtime that would really help me.

Answer

They're not random. They're the length of the following value. This is similar to common C++ name mangling and supports identifiers that may have fairly arbitrary characters in them without needing some new separator character. It also can make them easier to parse, especially in C.

In this particular case, it's _TtC then "an eleven character module name" then the module name and then "a fourteen character class name" and the class name. I assume C is class. Not sure about Tt (maybe "type").