標籤:des style blog io color ar os 使用 for
- @public、@protected、@private的使用
在OC中聲明一個類的時候,可以使用上面 @public、@protected、@private三個關鍵字聲明執行個體的許可權,例如下面的代碼:
#import <Foundation/Foundation.h>@interface Person : NSObject{@public NSString *_height;@protected NSString *_weight;@private NSString *_wife;}@end@interface Person (){ @public NSString *_name; @protected NSString *_sex; @private NSString *_phoneNO;}- (void)publicMethod;@end@implementation Person- (instancetype)init{ if(self = [super init]) { _height = @"1.8m"; _weight = @"65Kg"; _wife = @"none"; _name = @"zhangSan"; _sex = @"man"; _phoneNO = @"1122334455"; } return self;}- (void)publicMethod{ }@end
在.h檔案中定義了三個執行個體,這三個執行個體的許可權和public、protected、private相對應,對於一個Person類型的對象p,_name在任何地方都是可以訪問的,_sex只能在Person
的子類中訪問,_wife只能自己內部訪問。在.m檔案中定義的三個執行個體也與public、protected、private相對應,需要注意的是,在其它檔案中定義的Person類型的對象p,都不能訪問到這三個執行個體,只有在.m檔案內部,public、protected、private才會起到對應的作用。實際上OC中執行個體的許可權控制只是編譯時間的一種許可權限制,在OC runtime的類結構中,執行個體變數是不存在許可權控制的,同樣類方法、執行個體方法也不存在許可權控制。
- 對於KVC來說,無論執行個體是哪種許可權都是可以輕鬆訪問到的,如下面的代碼:
@interface Student : Person@end@implementation Student+ (void)testProtected{ Person *p = [Person new]; NSLog(@"%@",p->_height); NSLog(@"%@",p->_weight);}- (NSString *)description{ Person *p = [Person new]; NSLog(@"%@",p->_height); NSLog(@"%@",p->_weight);// NSLog(@"%@",p->_wife);// [p publicMethod];// [self publicMethod]; //return [NSString stringWithFormat:@"name == %@\nsex == %@\nphone == %@",_name, _sex,_phoneNO]// return [NSString stringWithFormat:@"name == %@\nsex == %@",_name, _sex];// return [NSString stringWithFormat:@"%@ , %@ , %@",_height , _weight ,_wife]; return [NSString stringWithFormat:@"%@ , %@",_height , _weight]; }@endKVC訪問:
#import "Other.h"#import "Person.h"@interface Other : NSObject- (void)testProtected;- (void)testKVC;@end@implementation Other- (void)testProtected{ Person *p = [Person new]; NSLog(@"%@",p->_height);// NSLog(@"%@",p->_weight);}- (void)testKVC{ Person *p = [Person new]; NSLog(@"%@",[p valueForKey:@"_name"]); NSLog(@"%@",[p valueForKey:@"_sex"]); NSLog(@"%@",[p valueForKey:@"_phoneNO"]); NSLog(@"%@",[p valueForKey:@"_wife"]);}@end實際KVC讀取一個執行個體變數:
- (id)valueForKey: (NSString*)key{ if (!key) { id value = [self valueForUndefinedKey:nil]; return value; } const char *keyCString = [key UTF8String]; SEL sel = sel_getUid(keyCString); // FIXME: getKey, _getKey, isKey, _isKey are missing if ([self respondsToSelector:sel]) { id value = [self _wrapReturnValueForSelector:sel]; return value; } size_t keyCStringLength = strlen(keyCString); char *selBuffer = __builtin_alloca(keyCStringLength + 5); char *keyname = __builtin_alloca(keyCStringLength + 1); strcpy(keyname, keyCString); #define TRY_FORMAT(format)\ sprintf(selBuffer, format, keyname); sel = sel_getUid(selBuffer); if ([self respondsToSelector:sel]) { id value = [self _wrapReturnValueForSelector:sel]; return value; } TRY_FORMAT("_%s"); keyname[0] = toupper(keyname[0]); TRY_FORMAT("is%s"); TRY_FORMAT("_is%s");// TRY_FORMAT("get%s");// TRY_FORMAT("_get%s"); #undef TRY_FORMAT if ([isa accessInstanceVariablesDirectly]) { sprintf(selBuffer, "_%s", keyCString); sel = sel_getUid(selBuffer); if ([self respondsToSelector:sel]) { id value = [self _wrapReturnValueForSelector:sel]; return value; } Ivar ivar = class_getInstanceVariable(isa, selBuffer); if (!ivar) { ivar = class_getInstanceVariable(isa, keyCString); } if (ivar) { id value = [self _wrapValue:(void*)self + ivar_getOffset(ivar) ofType:ivar_getTypeEncoding(ivar)]; return value; } } id value = [self valueForUndefinedKey:key]; return value;}
IOS中執行個體的許可權控制