ios 分类添加属性的问题

来源:互联网 发布:淘宝店铺租赁平台 编辑:程序博客网 时间:2024/06/05 07:14

首先,创建一个person类,代码如下:

XGPerson.h

#import <Foundation/Foundation.h>@interface XGPerson : NSObject/// 年龄@property (nonatomic, copy) NSString *age;/// 性别@property (nonatomic, copy) NSString *sex;- (void)text1;@end

XGPerson.m

#import "XGPerson.h"@implementation XGPerson- (void)text1 {    NSLog(@"%s",__func__);}- (void)text2 {    NSLog(@"%s",__func__);}@end

在控制器里获取并打印该类的成员变量、属性和方法,代码如下:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    // 获取成员变量    unsigned int ivarCount = 0;    Ivar *ivars = class_copyIvarList([XGPerson class], &ivarCount);    for (int i = 0; i < ivarCount; i++) {        Ivar ivar = ivars[i];        NSLog(@"第%d个成员变量:%s",i,ivar_getName(ivar));    }    free(ivars);    // 获取属性    unsigned int propertyCount = 0;    objc_property_t *propertyList = class_copyPropertyList([XGPerson class], &propertyCount);    for (int i = 0; i < propertyCount; i++) {        objc_property_t property = propertyList[i];        NSLog(@"第%d个属性:%s",i,property_getName(property));    }    // 获取方法列表    unsigned int methodCount = 0;    Method *methods = class_copyMethodList([XGPerson class], &methodCount);    for (int i = 0; i < methodCount; i++) {        Method method = methods[i];        NSLog(@"第%d个方法:%s",i, sel_getName(method_getName(method)));    }}

此时控制台输出如下:


没有分类时.png

平时使用@property的时候,系统会自动生成带“_”的成员变量和该变量的setter和getter方法。也就是说,属性相当于一个成员变量加getter和setter方法。那么,在分类里使用@property会是什么样子,创建一个分类

XGPerson+height.h

#import "XGPerson.h"@interface XGPerson (height)@property (nonatomic, copy) NSString *height;@end

XGPerson+height.m

#import "XGPerson+height.h"#import <objc/runtime.h>@implementation XGPerson (height)@end

如果像上面一样只在.h文件里声明height,那么.m文件里会出现两个警告,意思是说没有实现setter和getter方法。


警告.png

此时在控制器里执行touchesBegan方法,控制台输出如下:


有分类未实现存取方法.png

可以看到,此时person类里并没有添加带“_”的成员变量,也没有实现setter和getter方法,只是在属性列表里添加了height属性。并且此时如果在控制器里调用self.height,程序运行时会报错,显示找不到该方法。此时要达到添加的目的可以使用运行时的关联对象。示例代码如下:

XGPerson+height.m

#import "XGPerson+height.h"#import <objc/runtime.h>@implementation XGPerson (height) - (NSString *)height { return objc_getAssociatedObject(self, @"height"); } - (void)setHeight:(NSString *)height { objc_setAssociatedObject(self, @"height", height, OBJC_ASSOCIATION_COPY_NONATOMIC); }@end




原创粉丝点击