最近跟人交流時,提到一個問題,說iOS分類中不能添加屬性。這里探討一下不能添加的原因和添加的方法。
首先,創建一個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)));
}
}
此時控制臺輸出如下:
這里需要提出的是,平時使用@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方法。
此時在控制器里執行touchesBegan方法,控制臺輸出如下:
可以看到,此時person類里并沒有添加帶“_”的成員變量,也沒有實現setter和getter方法,只是在屬性列表里添加了height屬性。并且此時如果在控制器里調用self.height,程序運行時會報錯,顯示找不到該方法。實現一下person分類里的兩個方法:
XGPerson+height.m
#import "XGPerson+height.h"
#import <objc/runtime.h>
@implementation XGPerson (height)
- (NSString *)height {
}
- (void)setHeight:(NSString *)height {
}
@end
此時在控制器里執行touchesBegan方法,控制臺輸出如下:
可以看到即使實現了setter和getter方法,也仍然沒有添加帶“”的成員變量,也就是說,在setter和getter方法里仍然不能直接訪問以下劃線開頭的成員變量,因為在分類里用@property聲明屬性時系統并沒有添加以“”開頭的成員變量。此時要達到添加的目的可以使用運行時的關聯對象。示例代碼如下:
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
當然也可以在setter和getter方法里訪問該類其他的屬性,比如在UIView的分類的里添加x、y屬性,可以直接返回self.frame.origin.x和self.frame.origin.y。
總結
在分類里使用@property聲明屬性,只是將該屬性添加到該類的屬性列表,并聲明了setter和getter方法,但是沒有生成相應的成員變量,也沒有實現setter和getter方法。所以說分類不能添加屬性。但是在分類里使用@property聲明屬性后,又實現了setter和getter方法,那么在這個類以外可以正常通過點語法給該屬性賦值和取值。就是說,在分類里使用@property聲明屬性,又實現了setter和getter方法后,可以認為給這個類添加上了屬性。