How to decipher “objc_method_description” from protocol method description list?

我的梦境 提交于 2019-12-04 13:53:46

问题


I have some Swift 3 code to decode an iOS Objective-C protocol (which has a Swift counterpart). After concluding Swift 3 reflection was not developed enough to accomplish what I needed, I stumbled on the objc runtime method protocol_copyMethodDescriptionList(), which returns an array of the following C structs:

struct objc_method_description
     SEL name;
     char *types;
};

The code fetches a list of protocol selector names, but not sure what's being returned in the type field. I'm confused about how to properly decode objc_method_description.type values.

What I'm getting in the the type fields are in a cryptic format, for example, "B48@0:8@16{_NSRange=QQ}24@40" At first I thought it was a problem in how I was converting C strings, but after some study, I suspect it actually is an encoding of the parameters, similar how Java's JVM passes around method signatures. But I still have no reference by which to decode it.

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()
    }
}

The output of that in the XCode console is:

Optional(textViewDidBeginEditing:) Optional("v24@0:8@16")
Optional(textViewDidEndEditing:) Optional("v24@0:8@16")
Optional(textViewShouldBeginEditing:) Optional("B24@0:8@16")
Optional(textViewShouldEndEditing:) Optional("B24@0:8@16")
Optional(textView:shouldChangeTextInRange:replacementText:) Optional("B48@0:8@16{_NSRange=QQ}24@40")
Optional(textView:shouldChangeTextInRange:replacementText:) Optional("B48@0:8@16{_NSRange=QQ}24@40")
.
.
.


回答1:


What is returned objc_method_description.type field?

  • A method signature encoding scheme
  • ... which includes an obsolete stack offset representation

In other words, to get a usable method signature encoding from the type field, just just extract the alpha-symbolic characters in left-to-right order discarding the digits.

Supporting documentation:

  • Objective-C Method Signature Type Encoding
  • Meaning of the numeric elements in signature encoding, explained by an Apple developer
  • Playing around with Type Encodings
  • class_addmethod (according to @Richard_J_Ross, this is the root of it)
  • Objective-C Runtime Programming Guide
  • Objective-C Runtime Reference



来源:https://stackoverflow.com/questions/41502199/how-to-decipher-objc-method-description-from-protocol-method-description-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!