前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NSObject头文件解析 / 消息机制 / Runtime解读 (二)

NSObject头文件解析 / 消息机制 / Runtime解读 (二)

作者头像
周希
发布2019-10-15 01:29:03
5480
发布2019-10-15 01:29:03
举报

本章接着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);
    
}

运行结果:

2017-02-03 15:52:01.949 RunTimeDemo[786:37263] 添加成功
2017-02-03 15:52:01.949 RunTimeDemo[786:37263] 获取到的属性名为: 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, 4);

运行时创建新类:

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", 0);
    
    //注册类
    objc_registerClassPair(NewClass);
    
    //给类添加方法
    class_addMethod([NewClass class], @selector(printA), (IMP)printA, "v@:");
    
    //创建实例
    id ob = [NewClass new];
    
    //实例执行方法
    [ob printA];
    
}

运行结果:

2017-02-03 18:11:34.091 RunTimeDemo[1114:96700] Print A

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档