iOS 开发 Objective-C

iOS 底层 day03 isa superclass

2020-08-24  本文已影响0人  望穿秋水小作坊

isasupperclass 我们会基于下面的代码定义了两个类进行分析:
Person 继承自 NSObject,遵从 NSCopying 协议,有一个成员变量 _age,一个属性 personName,有一个实例方法和一个类方法。
Student 继承自 Person,遵从 NSCopying 协议,,有一个成员变量 _no,一个属性 studentName,有一个实例方法和一个类方法。

// Person
@interface Person : NSObject <NSCopying>
{
    @public
    int _age;
}
@property(nonatomic, assign) int personName;

- (void)personInstanceMethod;
+ (void)personClassMethod;

@end
@implementation Person
- (void)personInstanceMethod{
    NSLog(@"personInstanceMethod %p", self);
}
+ (void)personClassMethod{
    NSLog(@"personClassMethod %p", self);
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return self;
}

@end

// Student
@interface Student : Person <NSCoding>
{
    @public
    int _no;
}
@property(nonatomic, assign) int stuName;
- (void)studentInstanceMethod;
+ (void)studentClassMethod;

@end
@implementation Student
- (void)studentInstanceMethod{
    NSLog(@"studentInstanceMethod %p", self);
}
+ (void)studentClassMethod{
    NSLog(@"studentClassMethod %p", self);
}
- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
    return self;
}

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return self;
}

@end

一、 isa

isa 指针

二、 superclass

superclass

三、 isa 和 supperclass 最经典的图,没有之一

最经典的图,要求能自己手绘

由上图我们能得出哪些结论?

1. 从对象的角度来看,Objective-C 里面的对象分为哪三种?
  1. 实例对象(Instance Object)
  2. 类对象 (Class Object)
  3. 元类对象 (Meta-class Object)
2. 从 isa 角度看
3. 从 superclass 角度看
4. 从 method 调用角度看

四、 实践出真知,我们去 Xcode 中验证我们的猜想

1. 证实 Student实例的 isa 指向 Student的类对象
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *student = [[Student alloc] init];
        Class studentClass = [Student class];
        Class studentMetaClass = object_getClass(studentClass);
        NSLog(@"打断点位置");
    }
    return 0;
}

然后我们在控制台,打印如下调试信息:

(lldb) p/x student->isa
(Class) $7 = 0x001d800100002589 Student
(lldb) p/x studentClass
(Class) $8 = 0x0000000100002588 Student
(lldb) p/x 0x001d800100002589 & 0x00007ffffffffff8
(long) $9 = 0x0000000100002588 
2. 证实 Student的类对象 指向 Student的元类对象
(lldb) p/x studentClass->isa
error: member reference base type 'Class' is not a structure or union
OBJC2.0 的objc_class OBJC2.0 的objc_object
struct sp_objc_class {
    Class isa;
};
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *student = [[Student alloc] init];
        struct sp_objc_class *studentClass =(__bridge struct sp_objc_object *)[Student class];
        struct sp_objc_class *studentMetaClass = (__bridge struct sp_objc_object *)object_getClass([Student class]);
        NSLog(@"打断点位置");
    }
    return 0;
}
(lldb) p/x student->isa 
(Class) $3 = 0x001d800100002589 Student
(lldb) p/x 0x001d800100002589 & 0x00007ffffffffff8
(long) $4 = 0x0000000100002588
(lldb) p/x studentClass
(sp_objc_object *) $5 = 0x0000000100002588
(lldb) p/x studentClass->isa
(Class) $6 = 0x001d800100002561
(lldb) p/x 0x001d800100002561 & 0x00007ffffffffff8
(long) $7 = 0x0000000100002560
(lldb) p/x studentMetaClass
(sp_objc_object *) $8 = 0x0000000100002560
3. 代码证实 superclass 的指向
struct sp_objc_class {
    Class isa;
    Class superclass;
};

//#define ISA_MASK        0x00007ffffffffff8ULL
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *student = [[Student alloc] init];
        struct sp_objc_class *studentClass =(__bridge struct sp_objc_object *)[Student class];
        struct sp_objc_class *studentMetaClass = (__bridge struct sp_objc_object *)object_getClass([Student class]);
        
        Person *person = [[Person alloc] init];
        struct sp_objc_class *personClass =(__bridge struct sp_objc_object *)[Person class];
        struct sp_objc_class *personMetaClass = (__bridge struct sp_objc_object *)object_getClass([Person class]);
        
        NSLog(@"打断点位置");
    }
    return 0;
}
(lldb) p/x studentClass->superclass
(Class) $6 = 0x0000000100002540 Person
(lldb) p/x personClass
(sp_objc_class *) $7 = 0x0000000100002540
(lldb) p/x studentMetaClass->superclass
(Class) $8 = 0x0000000100002518
(lldb) p/x personMetaClass
(sp_objc_class *) $9 = 0x0000000100002518
4. 代码证实 Class 的内部结构,以及里面各个部分存放的内容
新版objc_class的结构体 class_rw_t 的内部结构 class_ro_t
#import <Foundation/Foundation.h>

#ifndef MJClassInfo_h
#define MJClassInfo_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 mj_objc_object {
    void *isa;
};

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

#endif /* MJClassInfo_h */

#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "MJClassInfo.h"

// Person
@interface Person : NSObject <NSCopying>
{
@public
    int _age;
}
@property(nonatomic, assign) int personName;

- (void)personInstanceMethod;
+ (void)personClassMethod;

@end
@implementation Person
- (void)personInstanceMethod{
    NSLog(@"personInstanceMethod %p", self);
}
+ (void)personClassMethod{
    NSLog(@"personClassMethod %p", self);
}
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return self;
}

@end

// Student
@interface Student : Person <NSCoding>
{
@public
    int _no;
}
@property(nonatomic, assign) int stuName;
- (void)studentInstanceMethod;
+ (void)studentClassMethod;

@end
@implementation Student
- (void)studentInstanceMethod{
    NSLog(@"studentInstanceMethod %p", self);
}
+ (void)studentClassMethod{
    NSLog(@"studentClassMethod %p", self);
}
- (void)encodeWithCoder:(nonnull NSCoder *)coder {
    
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
    return self;
}

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    return self;
}

@end


//#define ISA_MASK        0x00007ffffffffff8ULL
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *stu = [[Student alloc] init];
        mj_objc_class *studentClass = (__bridge mj_objc_class *)([Student class]);
        mj_objc_class *personClass = (__bridge mj_objc_class *)([Person class]);
        
        class_rw_t *studentClassData = studentClass->data();
        class_rw_t *personClassData = personClass->data();
        
        class_rw_t *studentMetaClassData = studentClass->metaClass()->data();
        class_rw_t *personMetaClassData = personClass->metaClass()->data();
        NSLog(@"打断点位置");
    }
    return 0;
}

类对象的`实例方法列表`,属性列表,协议列表 类对象的成员变量列表 元类对象的`类方法列表`
上一篇下一篇

猜你喜欢

热点阅读