iOS底层学习-day-15
前言-OC-runtime篇
我是一名iOS开发者, iOS底层 菜鸟的进阶之路30天。
问题
[(__bridge id)obj print];
- [(__bridge id)obj print]; 打印的内容
- (void)viewDidLoad {
[super viewDidLoad];
NSString *test = @"123";
id cls = [MJPerson class];
void *obj = &cls;//void是8个字节,就是isa指针
[(__bridge id)obj print];//123,找比cls地址高的8个字节
}
- [super viewDidLoad]; 的本质
//[super viewDidLoad];的本质
struct abc = {
self,
[ViewController class]
};
objc_msgSendSuper2(abc, sel_registerName("viewDidLoad"));
- 在编译器上的内容
// 局部变量分配在栈空间
// 栈空间分配,从高地址到低地址
void test() {
long long a = 4; // 0x7ffee638bff8
long long b = 5; // 0x7ffee638bff0
long long c = 6; // 0x7ffee638bfe8
long long d = 7; // 0x7ffee638bfe0
NSLog(@"%p %p %p %p", &a, &b, &c, &d);
}
下列打印的出的是什么
实例方法的isMemberOfClass和isKindOfClass
id person = [[MJPerson alloc] init];
NSLog(@"%d", [person isMemberOfClass:[MJPerson class]]);//1 -- 判断是不是person刚好等于[MJPerson class]它
NSLog(@"%d", [person isMemberOfClass:[NSObject class]]);//0
NSLog(@"%d", [person isKindOfClass:[MJPerson class]]);//1 -- 判断是不是person等于[MJPerson class]它或者他的父类,的父类等
NSLog(@"%d", [person isKindOfClass:[NSObject class]]); //1
- - (BOOL)isMemberOfClass:(Class)cls内部结构
- - (BOOL)isKindOfClass:(Class)cls内部结构
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
- (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
类方法的isMemberOfClass和isKindOfClass
NSLog(@"%d", [NSObject isMemberOfClass:[NSObject class]]); //0
NSLog(@"%d", [MJPerson isMemberOfClass:[MJPerson class]]); //0
// 这句代码的方法调用者不管是哪个类(只要是NSObject体系下的),都返回YES
NSLog(@"%d", [NSObject isKindOfClass:[NSObject class]]); //1 -- NSObject的元类或者的元类 是不是[NSObject class]
NSLog(@"%d", [MJPerson isKindOfClass:[MJPerson class]]); //0 -- MJPerson的元类或者的元类 是不是[MJPerson class]
- + (BOOL)isMemberOfClass:(Class)cls内部结构
- + (BOOL)isKindOfClass:(Class)cls内部结构
//类的object_getClass是基类
+ (BOOL)isMemberOfClass:(Class)cls {
return object_getClass((id)self) == cls;
}
+ (BOOL)isKindOfClass:(Class)cls { //判断self的类的父类,是不是传进来的子类或者类
for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}