相关疑难解决方法(0)

如何计算ObjC方法类型编码中的数字?

是对我上一个问题的后续跟踪: ObjC方法类型编码字符串中的数字是什么?

说有一个编码:

v24@0:4:8@12B16@20
Run Code Online (Sandbox Code Playgroud)

这些数字是如何计算的?B是一个字符,所以它应该只占用1个字节(而不是4个字节).它与"对齐"有关吗?大小是void多少?

如下计算数字是否正确?询问sizeof每个项目并将结果四舍五入到4的倍数?第一个数字成为所有其他数字的总和?

objective-c objective-c-runtime

12
推荐指数
2
解决办法
1354
查看次数

swift 块的 `objc_method_description.types` 的格式是什么?

Swift 编译器为带有块的 swift 方法生成的 Objective-C 方法编码似乎使用了任何地方都没有记录的语法。

例如方法:

func DebugLog2(message: String) async -> String
Run Code Online (Sandbox Code Playgroud)

有方法编码:

v32@0:8@"NSString"16@?<v@?@"NSString">24

并且文档中至少有三个字符("<和)未涵盖:>

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

该类NSMethodSignature也不喜欢这种编码:

[NSMethodSignature signatureWithObjCTypes:"\"NSString\"16@?<v@?@\"NSString\">24"];
Run Code Online (Sandbox Code Playgroud)

结果是:

'+[NSMethodSignaturesignatureWithObjCTypes:]: '"NSString"16@?<v@?@"NSString">24'' 中不支持类型编码规范 '"'

我尝试查看 Swift 的代码,似乎有一种叫做“扩展方法类型编码”的东西:

https://github.com/apple/swift/blob/c2fc7ee9e72e9a39854548e3202c667e4934dc65/test/IRGen/objc_methods.swift#L13-L14

但我在试图找出 Swift 代码库中方法类型编码生成的位置时迷失了方向。

不回答此问题的相关问题:

示例代码:

decodeProto()

@objc
class TestClass : NSObject {
    @objc
    func DebugLog(message: String) -> String {
        print(message)
        return message
    }

    @objc
    func DebugLog2(message: String) async -> String {
        do {
            try await Task.sleep(nanoseconds: 1_000_000_000)
        } catch {}
        print(message); …
Run Code Online (Sandbox Code Playgroud)

objective-c objective-c-runtime method-signature swift

6
推荐指数
1
解决办法
133
查看次数

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

我有一些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)

objective-c objective-c-runtime swift

3
推荐指数
1
解决办法
467
查看次数