前言

我们知道,OC 对象本质上是 C 语言结构体。那么 OC 对象转化成 C 语言底层是怎样的?今天我们就来盘一盘。

工具

借助 KSClassInfo.h :


#import <Foundation/Foundation.h>


#ifndef KSClassInfo_h
#define KSClassInfo_h

# if __arm64__
#   define ISA_MASK        0x0000000ffffffff8ULL
# elif __x86_64__
#   define ISA_MASK        0x00007ffffffffff8ULL
# endif

#if __LP64__
typedef uint32_t mask_t;
#else
typedef uint16_t mask_t;
#endif
typedef uintptr_t cache_key_t;

struct bucket_t {
    cache_key_t _key;
    IMP _imp;
};

struct cache_t {
    bucket_t *_buckets;
    mask_t _mask;
    mask_t _occupied;
};

struct entsize_list_tt {
    uint32_t entsizeAndFlags;
    uint32_t count;
};

struct method_t {
    SEL name;
    const char *types;
    IMP imp;
};

struct method_list_t : entsize_list_tt {
    method_t first;
};

struct ivar_t {
    int32_t *offset;
    const char *name;
    const char *type;
    uint32_t alignment_raw;
    uint32_t size;
};

struct ivar_list_t : entsize_list_tt {
    ivar_t first;
};

struct property_t {
    const char *name;
    const char *attributes;
};

struct property_list_t : entsize_list_tt {
    property_t first;
};

struct chained_property_list {
    chained_property_list *next;
    uint32_t count;
    property_t list[0];
};

typedef uintptr_t protocol_ref_t;
struct protocol_list_t {
    uintptr_t count;
    protocol_ref_t list[0];
};

struct class_ro_t {
    uint32_t flags;
    uint32_t instanceStart;
    uint32_t instanceSize;  // instance对象占用的内存空间
#ifdef __LP64__
    uint32_t reserved;
#endif
    const uint8_t * ivarLayout;
    const char * name;  // 类名
    method_list_t * baseMethodList;
    protocol_list_t * baseProtocols;
    const ivar_list_t * ivars;  // 成员变量列表
    const uint8_t * weakIvarLayout;
    property_list_t *baseProperties;
};

struct class_rw_t {
    uint32_t flags;
    uint32_t version;
    const class_ro_t *ro;
    method_list_t * methods;    // 方法列表
    property_list_t *properties;    // 属性列表
    const protocol_list_t * protocols;  // 协议列表
    Class firstSubclass;
    Class nextSiblingClass;
    char *demangledName;
};

#define FAST_DATA_MASK          0x00007ffffffffff8UL
struct class_data_bits_t {
    uintptr_t bits;
public:
    class_rw_t* data() {
        return (class_rw_t *)(bits & FAST_DATA_MASK);
    }
};

/* OC对象 */
struct ks_objc_object {
    void *isa;
};

/* 类对象 */
struct ks_objc_class : ks_objc_object {
    Class superclass;
    cache_t cache;
    class_data_bits_t bits;
public:
    class_rw_t* data() {
        return bits.data();
    }
    
    ks_objc_class* metaClass() {
        return (ks_objc_class *)((long long)isa & ISA_MASK);
    }
};

#endif /* KSClassInfo_h */

开始窥探

然后在 view controller 中窥探:

#import "ViewController.h"
#import "KSClassInfo.h"
#import <objc/runtime.h>


@interface Kyson : NSObject

@property (nonatomic, copy) NSString *sex;
@property (nonatomic) NSInteger age;
@property (nonatomic) NSInteger height;
@property (nonatomic, copy) NSString *tel;
@property (nonatomic, copy) NSString *IDNumber;

-(void) doSomething;

@end


@implementation Kyson

-(void) doSomething {
    NSLog(@"%s==%@",__func__,self);
}

@end


@interface ViewController ()



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    ks_objc_class *student_class = (__bridge ks_objc_class *) [Kyson class];
    class_rw_t *kyson_class_data = student_class->data();
    
    NSLog(@"1111");
}


@end

可以看到 C 语言层面的信息一览无余:

runtime