美文网首页
isa和superclass

isa和superclass

作者: SKY_Tang | 来源:发表于2018-09-10 16:48 被阅读0次

isa指针

#import <Foundation/Foundation.h>

@interface MJPerson : NSObject <NSCopying>

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

@end

@implementation MJPerson

+ (void)personClassMethod
{
    
}

- (void)personInstanceMethod
{
    
}

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

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJPerson *person = [[MJPerson alloc] init];
        [person personInstanceMethod];
        
        [MJPerson personClassMethod];
    }
    return 0;
}

将Objective-c代码转换成 C\C++的代码

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpps

找到[person personInstanceMethod];的 C++实现

((void (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("personInstanceMethod"));

找到[MJPerson personClassMethod];的 C++实现

((void (*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("MJPerson"), sel_registerName("personClassMethod"));

可以看出对象方法 C++实现是对instance对象发送一个方法消息,类方法 C++实现是对class对象发送一个方法消息。

但是对象方法是存放在class对象中,类方法是存放在meta-class对象中。也就是说instance对象找到class对象中的对象方法,class对象找到meta-class对象中的类方法。

isa指针
  1. 当调用对象方法时,通过instance对象的isa指针找到class对象,最后找到对象方法的实现进行调用
  2. 当调用类方法时,通过class对象的isa指针找到meta-class对象,最后找到类方法的实现进行调用

superclass指针

#import <Foundation/Foundation.h>

@interface MJPerson : NSObject <NSCopying>

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

@end

@implementation MJPerson

+ (void)personClassMethod
{
    
}

- (void)personInstanceMethod
{
    
}

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

@end

@interface MJStudent : MJPerson <NSCoding>

+ (void)studentClassMethod;
- (void)studentInstanceMethod;

@end

@implementation MJStudent

+ (void)studentClassMethod
{
    
}

- (void)studentInstanceMethod
{
    
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    return nil;
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        MJStudent *student = [[MJStudent alloc] init];
        
        [student personInstanceMethod];
        
        [MJStudent personClassMethod];
    }
    return 0;
}

找到[student personInstanceMethod];的 C++实现

((void (*)(id, SEL))(void *)objc_msgSend)((id)student, sel_registerName("personInstanceMethod"));

找到[MJStudent personClassMethod];的 C++实现

((void (*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("MJStudent"), sel_registerName("personClassMethod"));

[student personInstanceMethod];通过isa指针只能找到MJStudentclass对象,但是personInstanceMethod存储在MJPersonclass对象中。

[MJStudent personClassMethod];通过isa指针只能找到MJStudentmeta-class对象,但是personClassMethod存储在MJPersonmeta-class对象中。

这就需要MJStudentclass对象找到MJPersonclass对象中的对象方法,MJStudentmeta-class对象找到存储在MJPersonmeta-class对象中的类方法。

superclass

MJStudentinstance对象要调用MJPerson的对象方法时,会先通过isa找到MJStudentclass对象,然后通过superclass找到MJPersonclass对象,最后找到对象方法的实现进行调用。

superclass

MJStudentclass对象要调用MJPerson的类方法时,会先通过isa找到MJStudentmeta-class对象,然后通过superclass找到MJPersonmeta-class对象,最后找到类方法的实现进行调用。

isa 、superclass总结

isa 、superclass总结
  1. instance对象的isa指向class对象
  2. class对象的isa指向meta-class对象
  3. meta-class对象的isa指向基类的meta-class对象
  4. class对象的superclass指向父类的class对象

    如果没有父类,superclass指针为nil

  5. meta-class对象的superclass指向父类的meta-class对象

    基类的meta-class对象的superclass指向基类的class对象

  6. instance对象调用方法的轨迹

    isaclass对象,方法不存在,就通过superclass找父类
    最后方法不存在报错unrecognized selector send to instance *****

  7. class对象调用类方法的轨迹

    isameta-clas对象,方法不存在,就通过superclass找父类
    最后方法不存在报错unrecognized selector send to class *****

代码验证基类的meta-class对象的superclass指向基类的class对象
Nsobject的分类

#import "NSObject+test.h"

@implementation NSObject (test)

+ (void)test
{
    NSLog(@"+[NSObject test]----%p", self);
}

@end

main文件的代码

#import <Foundation/Foundation.h>
#import "NSObject+test.h"

@interface MJPerson : NSObject

+ (void)test;

@end

@implementation MJPerson

+ (void)test
{
    NSLog(@"+[MJPerson test]----%p", self);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"[MJPerson class]---%p", [MJPerson class]);
        NSLog(@"[NSObject class]---%p", [NSObject class]);
        
        [MJPerson test];
        [NSObject test];
    }
    return 0;
}

打印结果

验证

结果看出MJPersonclass对象调用+[MJPerson test]方法,NSObjectclass对象调用+[NSObject test]方法。

注释掉MJPerson类的+ (void)test;方法

#import <Foundation/Foundation.h>
#import "NSObject+test.h"

@interface MJPerson : NSObject

+ (void)test;

@end

@implementation MJPerson

//+ (void)test
//{
//    NSLog(@"+[MJPerson test]----%p", self);
//}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"[MJPerson class]---%p", [MJPerson class]);
        NSLog(@"[NSObject class]---%p", [NSObject class]);
        
        [MJPerson test];
        [NSObject test];
    }
    return 0;
}

打印结果

验证

结果看出MJPersonclass对象调用+[NSObject test]方法,NSObjectclass对象调用+[NSObject test]方法。

再注释掉NSObject类的- (void)test方法,添加- (void)test方法

#import "NSObject+test.h"

@implementation NSObject (test)

//+ (void)test
//{
//    NSLog(@"+[NSObject test]----%p", self);
//}

- (void)test
{
    NSLog(@"-[NSObject test]----%p", self);
}

@end

打印结果

验证

结果看出MJPersonclass对象调用-[NSObject test]方法,NSObjectclass对象调用-[NSObject test]方法。但是- (void)test方法是存放在NSObjectclass对象中,也就证明了基类的meta-class对象的superclass指向基类的class对象

相关文章

  • iOS-浅谈OC中isa和superclass的指针指向

    目录 isa指针----isa指针指向superclass指针----class对象的superclass指针--...

  • iOS:isa与superclass

    目录一,对象的三种类型二,对象的存储信息三,isa指针四,superclass指针五,isa和superclass...

  • 笔记 - isa和superclass

    isa的调用 class的superclass meta-class的superclass 总结1 总结2 isa...

  • isa和superclass

    isa指针 将Objective-c代码转换成 C\C++的代码 找到[person personInstance...

  • isa和SuperClass

    开局一张图 刚开始看这图时,我也是懵的 其实也很好理解,这得分开看 虚线 实线 根类、基类 一般都是指:NSObj...

  • isa和superClass

    isa 对象的isa指针是指向哪里的? instance的 isa 指向 class当调用对象方法时,通过inst...

  • isa和superclass

    D、题:对象的isa指针指向哪里? 拓展: class对象的superclass指针指向哪里? meta-clas...

  • isa 和 superclass

    OC对象的分类OC中的对象,简单来说可以分为三大类:instance 对象(实例对象)、class 对象(类对象)...

  • isa 和 superclass

    实例对象的 isa 指向类对象,类对象的 isa 指向元类对象,元类的isa 指向基类的元类对象,基类的元类对象的...

  • isa和superClass

    Objective-C的本质 我们平时编写的Objective-C代码,底层实现其实都是C\C++代码,Objec...

网友评论

      本文标题:isa和superclass

      本文链接:https://www.haomeiwen.com/subject/tzkewftx.html