OC对象底层原理

apple open source: https://opensource.apple.com/tarballs/

alloc流程:

什么是runtime: c c++ 汇编写的api,c过渡到oc的运行时,体现runtime的学习和应用

objec_msgSend(id 消息接收者, sel) --- 发送消息

alloc 通过我们的类创建实例对象

init 为什么还需要, 啥也没做,直接返回self---是一种设计模式,为了自由(为了重写,根据需求写,可以不重写)

LLVM优化-什么?

扫描二维码关注公众号,回复: 7919738 查看本文章

编译,链接,运行,空闲

内存对齐(8位):(num+7)>>3<<3 结果返回8倍数,舍掉末尾。(空间换时间)

通过运行时创建类:

Class tempClass = objc_allocateClassPair([NSObject class], "TempClass", 0);

//添加属性, 必须先添加后注册,因为是只读ro(read only)

Class TempClass = objc_allocateClassPair([NSObject class], "TempClass", 0);

//添加属性

NSString *name = @"name";

objc_registerClassPair(TempClass);

class_addIvar(TempClass, name.UTF8String, sizeof(id), log2(sizeof(id)), @encode(id));

id p = [TempClass alloc];

[p setValue:@"fanxing" forKey:@"name"];

NSLog(@"name == %@", [p valueForKey:name]);

实例方法获取--对象实例方法-- 类

类方法--元类

猜你喜欢

转载自www.cnblogs.com/coolcold/p/11871021.html