简体   繁体   English

Swift Objective-C运行时类命名

[英]Swift Objective-C runtime class naming

I've noticed that a Swift Class is renamed in objective-c runtime. 我注意到在Objective-c运行时重命名了Swift类。 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 . 所以,如果我在swift中有一个名为ViewController的类,当我执行object_getClass(self)时,我的应用程序的名称是TestRuntime ,我自己就是ViewController ,我会得到: _TtC11TestRuntime14ViewController I've noticed this pattern or format: _TtC$$AppName$$ClassName , $ being a random number. 我注意到了这种模式或格式: _TtC$$AppName$$ClassName$是一个随机数。 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. 如果有人能够了解swift如何快速命名它在运行时的类,那将真正帮助我。

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. 这类似于常见的C ++名称修改,并支持可能在其中包含相当任意字符的标识符,而不需要一些新的分隔符。 It also can make them easier to parse, especially in C. 它还可以使它们更容易解析,尤其是在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. 在这种特殊情况下,它是_TtC然后是“十一个字符模块名称”然后是模块名称,然后是“十四个字符类名”和类名。 I assume C is class. 我假设C是上课。 Not sure about Tt (maybe "type"). 不确定Tt (也许是“类型”)。

The description is build up in the following 3 sections 描述在以下3个部分中构建

  1. The description starts with _Tt which stands for target (bundle / app name) 描述以_Tt开头,它代表目标(包/应用程序名称)
  2. After that you will have one or more letters like CPF which stands for Class, Protocol or Function. 之后,您将拥有一个或多个字母,如CPF,代表类,协议或功能。 These are in reverse order according to the nesting 根据嵌套,它们的顺序相反
  3. Then you will get series of numbers plus a name where the number is the length of the name. 然后,您将获得一系列数字以及一个名称,其中数字是名称的长度。 each of these are for the target, Class, Protocol or Function as specified in the beginning of the description. 这些中的每一个都用于描述开头所指定的目标,类,协议或功能。

Functions are a special case here. 功能是这里的特例。 The description will also has some information about the function signature. 描述还将包含有关函数签名的一些信息。

For example you could have a _TtCFCC5MyApp7MyClass10MySubClass6myFuncFS0_FT_T_L_11MySubSubClass 例如,您可以拥有_TtCFCC5MyApp7MyClass10MySubClass6myFuncFS0_FT_T_L_11MySubSubClass

This would be the description of the MySubSubClass in the following code: 这将是以下代码中MySubSubClass的描述:

class MyClass {
   class MySubClass {
     func myFunc() {
       class MySubSubClass {
       }
     }
   }
}

Here you can find some sample code that will parse that description into easy to use properties and arrays. 在这里,您可以找到一些示例代码 ,将该描述解析为易于使用的属性和数组。

Update: Demangle is now converted to swift. 更新:Demangle现在转换为swift。 You can find it here: https://github.com/mattgallagher/CwlDemangle/blob/master/CwlDemangle/CwlDemangle.swift 你可以在这里找到它: https//github.com/mattgallagher/CwlDemangle/blob/master/CwlDemangle/CwlDemangle.swift

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM