iOS 开发 Objective-C

iOS 底层 day14 super isMemberOfCla

2020-09-07  本文已影响0人  望穿秋水小作坊
@interface YYPerson: NSObject
@end

@interface YYStudent : YYPerson
@end

一、super 关键字

1. 如下代码,你能回说出输出日志吗?
@implementation YYStudent
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSLog(@"[self class] = %@", [self class]);
        NSLog(@"[super class] = %@", [super class]);
        NSLog(@"[self superclass] = %@", [self superclass]);
        NSLog(@"[super superclass] = %@", [super superclass]);
    }
    return self;
}
@end
2. 首先,这种问题,我们不能靠猜,我们应该在脑海中有清晰的认知,它们底层到底干了啥?
static void _I_YYStudent_test(YYStudent * self, SEL _cmd) {
    //[self class]
    objc_msgSend(self, sel_registerName("class"));
    //[super class]
    objc_msgSendSuper((__rw_objc_super){
            (id)self,
            (id)class_getSuperclass(objc_getClass("YYStudent"))
        },
        sel_registerName("class"));
}
/**
 * Sends a message with a simple return value to the superclass of an instance of a class.
 *
 * @param super A pointer to an \c objc_super data structure. Pass values identifying the
 *  context the message was sent to, including the instance of the class that is to receive the
 *  message and the superclass at which to start searching for the method implementation.
 *
 * @param op A pointer of type SEL. Pass the selector of the method that will handle the message.
 */
objc_msgSendSuper(struct objc_super * _Nonnull super, SEL _Nonnull op, ...)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

struct objc_super {
    /// Specifies an instance of a class.
    __unsafe_unretained _Nonnull id receiver;

    /// Specifies the particular superclass of the instance to message.
    __unsafe_unretained _Nonnull Class super_class;
};
3. 问题1解答
@implementation YYStudent
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSLog(@"[self class] = %@", [self class]);
        NSLog(@"[super class] = %@", [super class]);
        NSLog(@"[self superclass] = %@", [self superclass]);
        NSLog(@"[super superclass] = %@", [super superclass]);
    }
    return self;
}
@end
Demo[15383:867835] [self class] = YYStudent
Demo[15383:867835] [super class] = YYStudent
Demo[15383:867835] [self superclass] = YYPerson
Demo[15383:867835] [super superclass] = YYPerson

二、isKindOfClassisMemberOfClass

1. 思考如下代码的输出
int main(int argc, const char * argv[]) {
    BOOL b1 = [[YYPerson class] isKindOfClass:[YYPerson class]];
    BOOL b2 = [[YYPerson class] isMemberOfClass:[YYPerson class]];
    BOOL b3 = [[NSObject class] isKindOfClass:[NSObject class]];
    BOOL b4 = [[NSObject class] isKindOfClass:[NSObject class]];
    NSLog(@"%d, %d, %d, %d", b1, b2, b3, b4);
    return 0;
}
2. 同样的原理,我们回答问题之前,先要知道 isKindOfClassisMemberOfClass 底层到底调用了什么方法?
+ (BOOL)isMemberOfClass:(Class)cls {
    return object_getClass((id)self) == cls;
}

- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}

+ (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

- (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}
3. 练习巩固,下面代码的输出?
int main(int argc, const char * argv[]) {
    YYPerson *person = [[YYPerson alloc] init];
    NSLog(@"%d", [person isMemberOfClass:[YYPerson class]]);
    NSLog(@"%d", [person isMemberOfClass:[NSObject class]]);
    NSLog(@"%d", [person isKindOfClass:[YYPerson class]]);
    NSLog(@"%d", [person isKindOfClass:[NSObject class]]);
    return 0;
}
Demo[16964:980177] 1 // 1 等价于: [person class] == [YYPerson class]
Demo[16964:980177] 0 // 0 等价于: [person class] == [NSObject class]
Demo[16964:980177] 1 // 1 等价于: YYPerson类对象以及父类对象  == YYPerson类对象
Demo[16964:980177] 1 // 1 等价于: YYPerson类对象以及父类对象  == NSObject类对象
4. 练习巩固,下面代码的输出?
int main(int argc, const char * argv[]) {
    NSLog(@"%d", [YYPerson isMemberOfClass:object_getClass([YYPerson class])]);
    NSLog(@"%d", [YYPerson isKindOfClass:object_getClass([NSObject class])]);
    return 0;
}
Demo[17167:991810] 1 // 1 等价于: YYPerson元类对象  == YYPerson元类对象
Demo[17167:991810] 1 // 1 等价于: YYPerson元类对象以及父元类对象  == NSObject元类对象
5. 练习巩固,下面代码的输出?
int main(int argc, const char * argv[]) {
    NSLog(@"%d", [[YYPerson class] isKindOfClass:[YYPerson class]]);
    NSLog(@"%d", [[YYPerson class] isMemberOfClass:[YYPerson class]]);
    NSLog(@"%d", [[NSObject class] isKindOfClass:[NSObject class]]);
    NSLog(@"%d", [[NSObject class] isMemberOfClass:[NSObject class]]);
    return 0;
}
Demo[16797:971234] 0 // 0 等价于: YYPerson的元类以及父元类 == YYPerson的类对象
Demo[16797:971234] 0 // 0 等价于: YYPerson的元类 == YYPerson的类对象
Demo[16797:971234] 1 // 1 等价于: NSObject的元类以及父元类 == NSObject的类对象 (根元类superclass 指向 NSObject的类对象)
Demo[16797:971234] 0 // 0 等价于: NSObject的元类 == YYPerson的类对象
上一篇下一篇

猜你喜欢

热点阅读