我正在阅读Apple 关于Objective-C运行时类型编码字符串的文章 ,有些方法在其类型字符串中有数字.
数字v12@0:4@8是什么意思?
是对我上一个问题的后续跟踪: ObjC方法类型编码字符串中的数字是什么?
说有一个编码:
v24@0:4:8@12B16@20
Run Code Online (Sandbox Code Playgroud)
这些数字是如何计算的?B是一个字符,所以它应该只占用1个字节(而不是4个字节).它与"对齐"有关吗?大小是void多少?
如下计算数字是否正确?询问sizeof每个项目并将结果四舍五入到4的倍数?第一个数字成为所有其他数字的总和?
我有一些Swift 3代码来解码iOS Objective-C协议(有一个Swift对应版本)。在总结完Swift 3反射后,我发现反射还不够完善,无法满足我的需要,我偶然发现了objc运行时方法protocol_copyMethodDescriptionList(),该方法返回以下C结构的数组:
struct objc_method_description
SEL name;
char *types;
};
Run Code Online (Sandbox Code Playgroud)
该代码获取协议选择器名称的列表,但不确定在该type字段中返回什么。我对如何正确解码objc_method_description.type值感到困惑。
什么我得到在该type领域是一个神秘的格式,例如,"B48@0:8@16{_NSRange=QQ}24@40"起初我还以为是在我是如何转换C字符串的一个问题,但一些研究之后,我怀疑它实际上是参数的编码,类似Java的JVM如何传递方法签名。但是我仍然没有参考来解码它。
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var noteView : UITextView!
func decodeProto() {
var methodCount : UInt32 = 1
if let methodList = protocol_copyMethodDescriptionList(UITextViewDelegate.self,
false, true, UnsafeMutablePointer<UInt32>(&methodCount)) {
for i in 0..<Int(methodCount) {
let methodDesc = methodList[i];
let name = methodDesc.name
let types = String(validatingUTF8: methodDesc.types)
print("\(name) \(types)")
}
}
}
override …Run Code Online (Sandbox Code Playgroud)