swift-objc-dump

一个用于导出 objective-c 类/协议/方法/属性/成员变量信息的 Swift 库。

Github issues Github forks Github stars Github top language

用法

可以按如下方式获取 Objective-C 类/协议/方法/属性/成员变量的信息。

import ObjCDump

// Class
let info = classInfo(of: NSObject.self)
let methods = info.methods
/* or */
let methods = methods(of: NSObject.self, isInstance: true)

// Protocol
let `prtocol` = NSProtocolFromString("NSCoding")!
let info = protocolInfo(of: `protocol`)
let methods = info.optionalMethods
/* or */
let methods = methods(of: `protocol`, isRequired: false, isInstance: true)

// Method
let method = class_getInstanceMethod(NSObject.self, NSSelectorFromString("init"))!
let info = ObjCMethodInfo(method)

// Property
let property = class_getProperty(NSObject.self, "classDescription")!
let info = ObjCPropertyInfo(method)

// Ivar
let ivar: Ivar = /**/
let info = ObjCIvarInfo(ivar)

头文件字符串

可以按如下方式输出 Objective-C 头文件。

import ObjCDump

let info = classInfo(of: NSObject.self)
print(info.headerString)

/*
@interface NSObject <CARenderValue, CAAnimatableValue, NSObject> {
    Class isa;
}

@property(class, readonly) BOOL accessInstanceVariablesDirectly;

@property(readonly, copy) NSClassDescription *classDescription;
@property(readonly, copy) NSArray *attributeKeys;

/* ...... */

+ (BOOL)xr_object:(id)arg0 isEqual:(id)arg1;
+ (void)dealloc;
+ (id)description;
+ (void)doesNotRecognizeSelector:(SEL)arg0;
+ (id)init;
+ (id)instanceMethodSignatureForSelector:(SEL)arg0;
+ (void)load;
+ (id)methodSignatureForSelector:(SEL)arg0;
+ (id)__allocWithZone_OA:(struct _NSZone {  } *)arg0;

/* ...... */

- (id)performSelector:(SEL)arg0 withObject:(id)arg1 withObject:(id)arg2;
- (BOOL)respondsToSelector:(SEL)arg0;
- (id)retain;
- (unsigned long long)retainCount;
- (BOOL)retainWeakReference;
- (id)self;
- (Class)superclass;
- (struct _NSZone {  } *)zone;

@end
*/

Objective-C 类型编码

在 ObjC 运行时获取的类型信息是被编码的。这与在 Objective-C 中使用 @encode 指令可以获得的转换相同。

参考

由于没有已发布的函数来解码这种编码的类型信息,因此实现了一个新的解码器。它可以按如下方式使用

import ObjCTypeDecodeKit

let type = ObjCTypeDecoder.decode("(?=iI{?=i{CGRect={CGPoint=dd}{CGSize=dd}}})")

print(type.decoded())
/*
union {
    int x0;
    unsigned int x1;
    struct {
        int x0;
        struct CGRect {
            struct CGPoint {
                double x0;
                double x1;
            } x0;
            struct CGSize {
                double x0;
                double x1;
            } x1;
        } x1;
    } x2;
}
*/

许可协议

swift-objc-dump 基于 MIT 许可协议发布。请参阅 许可协议