首先尔艇,創(chuàng)建一個(gè)person類裹赴,代碼如下:
.h
#import <Foundation/Foundation.h>
@interface XGPerson : NSObject
/// 年齡
@property (nonatomic, copy) NSString *age;
/// 性別
@property (nonatomic, copy) NSString *sex;
- (void)text1;
@end
.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個(gè)成員變量:%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個(gè)屬性:%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個(gè)方法:%s",i, sel_getName(method_getName(method)));
}
}
這里需要提出的是棋返,平時(shí)使用@property的時(shí)候延都,系統(tǒng)會(huì)自動(dòng)生成帶“_”的成員變量和該變量的setter和getter方法。也就是說(shuō)睛竣,屬性相當(dāng)于一個(gè)成員變量加getter和setter方法晰房。那么,在分類里使用@property會(huì)是什么樣子呢射沟,下面來(lái)創(chuàng)建一個(gè)分類:
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文件里會(huì)出現(xiàn)兩個(gè)警告,意思是說(shuō)沒(méi)有實(shí)現(xiàn)setter和getter方法验夯。
此時(shí)在控制器里執(zhí)行touchesBegan方法猖吴,控制臺(tái)輸出如下:
可以看到,此時(shí)person類里并沒(méi)有添加帶“_”的成員變量挥转,也沒(méi)有實(shí)現(xiàn)setter和getter方法海蔽,只是在屬性列表里添加了height屬性共屈。并且此時(shí)如果在控制器里調(diào)用self.height,程序運(yùn)行時(shí)會(huì)報(bào)錯(cuò)党窜,顯示找不到該方法拗引。實(shí)現(xiàn)一下person分類里的兩個(gè)方法:
XGPerson+height.m
#import "XGPerson+height.h"
#import <objc/runtime.h>
@implementation XGPerson (height)
- (NSString *)height {
}
- (void)setHeight:(NSString *)height {
}
@end
此時(shí)在控制器里執(zhí)行touchesBegan方法,控制臺(tái)輸出如下:
可以看到即使實(shí)現(xiàn)了setter和getter方法刑然,也仍然沒(méi)有添加帶“”的成員變量寺擂,也就是說(shuō),在setter和getter方法里仍然不能直接訪問(wèn)以下劃線開(kāi)頭的成員變量泼掠,因?yàn)樵诜诸惱镉聾property聲明屬性時(shí)系統(tǒng)并沒(méi)有添加以“”開(kāi)頭的成員變量怔软。此時(shí)要達(dá)到添加的目的可以使用運(yùn)行時(shí)的關(guān)聯(lián)對(duì)象。示例代碼如下:
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
當(dāng)然也可以在setter和getter方法里訪問(wèn)該類其他的屬性择镇,比如在UIView的分類的里添加x挡逼、y屬性,可以直接返回self.frame.origin.x和self.frame.origin.y腻豌。
總結(jié)
在分類里使用@property聲明屬性家坎,只是將該屬性添加到該類的屬性列表,并聲明了setter和getter方法吝梅,但是沒(méi)有生成相應(yīng)的成員變量虱疏,也沒(méi)有實(shí)現(xiàn)setter和getter方法。所以說(shuō)分類不能添加屬性苏携。但是在分類里使用@property聲明屬性后做瞪,又實(shí)現(xiàn)了setter和getter方法,那么在這個(gè)類以外可以正常通過(guò)點(diǎn)語(yǔ)法給該屬性賦值和取值右冻。就是說(shuō)装蓬,在分類里使用@property聲明屬性,又實(shí)現(xiàn)了setter和getter方法后纱扭,可以認(rèn)為給這個(gè)類添加上了屬性牍帚。