本章接着NSObject头文件解析 / 消息机制 / Runtime解读(一)写

给类添加属性:

BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)

其中有一个参数我们再在上一篇中提到过

typedef struct {

const char *name;           /**< The name of the attribute */

const char *value;          /**< The value of the attribute (usually empty) */

} objc_property_attribute_t;

我们看下常见的赋值

常用attribute name value
nonatomic "N" ""
strong/retain "&" ""
weak "W" ""
属性的类型type "T" "@TypeName", eg"@\"NSString\""
属性对应的实例变量Ivar "V" "Ivar_name", eg "_name"
readonly "R" ""
getter "G" "GetterName", eg"isRight"
setter "S" "SetterName", eg"setName"
assign/atomic 默认即为assign和retain    

例子:

- (void)viewDidLoad {

    [super viewDidLoad];

    //创建一个对象
ClassA *aClass = [ClassA new]; //创建一个attributes
objc_property_attribute_t nonatmoic = {"N", ""};
objc_property_attribute_t strong = {"&", ""};
objc_property_attribute_t type = {"T", "@\"NSString\""};
objc_property_attribute_t ivar = {"V", "_name"}; objc_property_attribute_t attributes[] = {nonatmoic, strong, type, ivar, getter, setter}; //给对象类添加属性
BOOL result = class_addProperty([aClass class], "name", attributes, 4); if (result) { NSLog(@"添加成功");
} else { NSLog(@"添加失败");
} //读取添加属性
objc_property_t property = class_getProperty([aClass class], "name"); //获取添加的属性的名称
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)]; //打印获取到的属性名
NSLog(@"获取到的属性名为: %@", propertyName); }

运行结果:

-- ::01.949 RunTimeDemo[:] 添加成功
-- ::01.949 RunTimeDemo[:] 获取到的属性名为: name

替换属性:

void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)

例子:

//创建一个对象
ClassA *aClass = [ClassA new]; //创建一个attributes
objc_property_attribute_t nonatmoic = {"N", ""};
objc_property_attribute_t strong = {"&", ""};
objc_property_attribute_t type = {"T", "@\"NSString\""};
objc_property_attribute_t ivar = {"V", "_name"}; objc_property_attribute_t attributes[] = {nonatmoic, strong, type, ivar}; //替换属性
class_replaceProperty([aClass class], "name", attributes, );

运行时创建新类:

Class objc_allocateClassPair(Class superclass, const char *name,
size_t extraBytes)

注册创建的新类:

void objc_registerClassPair(Class cls) 

例子:

void printA(){

    NSLog(@"Print A");
} - (void)viewDidLoad { [super viewDidLoad]; //创新新类
Class NewClass = objc_allocateClassPair([NSObject class], "NewObject", ); //注册类
objc_registerClassPair(NewClass); //给类添加方法
class_addMethod([NewClass class], @selector(printA), (IMP)printA, "v@:"); //创建实例
id ob = [NewClass new]; //实例执行方法
[ob printA]; }

运行结果:

-- ::34.091 RunTimeDemo[:] Print A

先Mark在这里, 后面会继续更新

最新文章

  1. codeforces 446B(优先队列)
  2. 用手机地图GPS导航费流量吗?
  3. 由验证码和session丢失的引发原因
  4. java中将一个字符数组赋值给另一个,两者同时变化
  5. (转)The Road to TensorFlow
  6. keras04 - 阿狗阿猫识别 面向对象编程
  7. springboot+thymeleaf+pageHelper带条件分页查询
  8. kernel中,dump_stack打印调用栈,print_hex_dump打印一片内存,记录一下
  9. 【github&amp;&amp;git】6、SmartGit(试用期30后),个人继续使用的方法。
  10. python 参数解析ArgumentParser
  11. Axis2之wsdl2java工具
  12. 【转】org.jdom.IllegalDataException: The data &quot;&quot;is not legal for a JDOM attribute: 0xb is not a legal 异常
  13. Android如何设置TextView的行间距、行高。
  14. 基于jquery下拉列表树插件代码
  15. CCSUOJ评测系统——第三次scrum冲刺
  16. luogu P2066 机器分配[背包dp+方案输出]
  17. _vsnprintf在可变参数打印中的用法
  18. keepalived+MySQL双主搭建
  19. LeetCode记录之13——Roman to Integer
  20. 启动PyCharm cannot start under Java 1.7 : Java 1.8 or later is required 解决方案

热门文章

  1. win7安装laravel
  2. mongodb文档支持的数据类型
  3. .NET Framework 3.5-8 下载地址
  4. PyQt4 UI设计和调用 使用eric6
  5. 前端基础之css样式(选择器)
  6. XSS - 禁止浏览器读取Cookie - HttpOnly
  7. LeetCode:对角线遍历【498】
  8. KVM虚拟化安装配置
  9. HackerRank - maximum-perimeter-triangle 【水】
  10. linux环境变量 【转】