在OC每個(gè)類中,我們通常會(huì)根據(jù)自己的需要扣典,添加自己需要的屬性妆毕,對(duì)于我們不熟知的類和協(xié)議,我們可以通過(guò)class_copyPropertyList和protocol_copyPropertyList得到一個(gè)包含一個(gè)類所有屬性的數(shù)組
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
舉個(gè)例子
@interface lender : NSObject
@property (nonatomic, strong)NSArray *nameArray;
@property (nonatomic, strong)NSString *name;
@property(nonatomic,assign) NSInteger age;
@end
id lenderClass=objc_getClass("lender");// 獲取lender類
unsigned int outCount,i;
objc_property_t*properties=class_copyPropertyList(lenderClass, &outCount);//得到屬性數(shù)組
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));//標(biāo)準(zhǔn)化輸出贮尖,stdout 是一個(gè)系統(tǒng)的宏定義
}
運(yùn)行結(jié)果
nameArray? T@"NSArray",&,N,V_nameArray
name? T@"NSString",&,N,V_name
age? Tq,N,V_age