如何从协议方法描述列表中解密“ objc_method_description”?

cle*_*ght 3 objective-c objective-c-runtime swift

我有一些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 func viewDidLoad() {
        super.viewDidLoad()
        decodeProto()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
Run Code Online (Sandbox Code Playgroud)

XCode控制台中的输出为:

可选(textViewDidBeginEditing:)可选(“ v24 @ 0:8 @ 16”)
可选(textViewDidEndEditing:)可选(“ v24 @ 0:8 @ 16”)
可选(textViewShouldBeginEditing:)可选(“ B24 @ 0:8 @ 16”)
可选(textViewShouldEndEditing:)可选( “ B24 @ 0:8 @ 16”)
可选(textView:shouldChangeTextInRange:replacementText:)可选(“ B48 @ 0:8 @ 16 {_NSRange = QQ} 24 @ 40”)
可选(textView:shouldChangeTextInRange:replacementText :)可选(“ B48 @ 0:8 @ 16 {_NSRange = QQ} 24 @ 40“)


cle*_*ght 5

返回的objc_method_description.type字段是什么?

  • 方法签名编码方案
  • ...其中包括过时的堆栈偏移量表示

换句话说,要从该字段中获得可用的方法签名编码type,只需按从左到右的顺序提取字母符号就可以丢弃数字。

支持文档: