廢話不多收愉舔,直接上代碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
.m文件
#import"Object.h"
@interfaceObject()
{
NSString*_objectStr_M;
NSDictionary*_objectDic_M;
NSNumber*_objectNum_M;
}
@property(nonatomic,strong)NSString*objectStr_propertyM;
@property(nonatomic,strong)NSDictionary*objectDic_propertyM;
@property(nonatomic,strong)NSNumber*objectNum_propertyM;
@end
@implementationObject
{
NSString*objectStr_implementation;
NSDictionary*objectDic_implementation;
NSNumber*objectNum_M;
}
-(instancetype)init
{
self=[superinit];
if(self) {
_objectNum_M=@2;
objectNum_M=@1;
objectStr_implementation=@"objectStr_implementation";
_objectStr_M=@"_objectStr_M";
_objectStr_h=@"_objectStr_h";
_objectStr_propertyh=@"__objectStr_propertyh";
_objectStr_propertyM=@"__objectStr_propertyM";
}
returnself;
}
.h文件
@interfaceObject :NSObject
{
NSString*_objectStr_h;
NSDictionary*_objectDic_h;
NSNumber*_objectNum_h;
}
@property(nonatomic,strong)NSString*objectStr_propertyh;
@property(nonatomic,strong)NSDictionary *objectDic_propertyh;
@property(nonatomic,strong)NSNumber*objectNum_propertyh;
@end
測試代碼:
Object*obj=[[Objectalloc]init];
if([objvalueForKey:@"_objectNum_M"])
NSLog(@"_objectNum_M is ______%@",[obj valueForKey:@"_objectNum_M"]);
if([objvalueForKey:@"objectNum_M"])
NSLog(@"objectNum_M is *******%@",[obj valueForKey:@"objectNum_M"]);
if([objvalueForKey:@"_objectStr_M"])
NSLog(@"_objectStr_M is %@",[obj valueForKey:@"_objectStr_M"]);
if([objvalueForKey:@"_objectStr_h"])
NSLog(@"_objectStr_h is %@",[obj valueForKey:@"_objectStr_h"]);
if([objvalueForKey:@"_objectStr_propertyh"])
NSLog(@"_objectStr_propertyh is %@",[obj valueForKey:@"_objectStr_propertyh"]);
if([objvalueForKey:@"_objectStr_propertyM"])
NSLog(@"_objectStr_propertyM is %@",[obj valueForKey:@"_objectStr_propertyM"]);
打印結果:
2016-09-08 23:17:17.759 PopMenu[2743:737039] _objectNum_M is ______2
2016-09-08 23:17:17.759 PopMenu[2743:737039] objectNum_M is *******2
2016-09-08 23:17:17.760 PopMenu[2743:737039] _objectStr_M is _objectStr_M
2016-09-08 23:17:17.760 PopMenu[2743:737039] _objectStr_h is _objectStr_h
2016-09-08 23:17:17.760 PopMenu[2743:737039] _objectStr_propertyh is __objectStr_propertyh
2016-09-08 23:17:17.760 PopMenu[2743:737039] _objectStr_propertyM is __objectStr_propertyM
可是這兩句出了問題
2016-09-08 23:17:17.759 PopMenu[2743:737039] _objectNum_M is ______2
2016-09-08 23:17:17.759 PopMenu[2743:737039] objectNum_M is *******2
一開始猜想是_objectNum_M?系統(tǒng)認為是同一變量,但編譯器不報錯,于是加了一句
這就證明 KVC可以獲取所有成員變量和屬性的指針
if([objvalueForKey:@"objectStr_implementation"])
NSLog(@"objectStr_implementation is %@",[objvalueForKey:@"objectStr_implementation"]);
發(fā)現(xiàn) ? ???2016-09-08 23:17:17.759 PopMenu[2743:737039] objectStr_implementation is objectStr_implementation
猜想錯誤炼团,于是又猜 系統(tǒng)認為_objectNum_M 猴蹂,objectNum_M是同一變量埠褪,于是交換_objectNum_M岖妄,objectNum_M? 在+(void)init方法里的賦值順序,結果還是一樣彻坛,于是又猜顷啼,可能是 _objectNum_M 聲明interfaceObject()踏枣,objectNum_M? 聲明在implementationObject里面,優(yōu)先級不一樣钙蒙,可是結果還是一樣茵瀑,這就證明系統(tǒng)在kvc取值時,_XXX 變量 優(yōu)先級>XXX 變量躬厌。這個測試還證明了KVC 權限很高马昨,可以取所有成員和屬性的指針。如果用下面這種方式聲名扛施,則不能訪問: