Permissions control for instances in iOS

Source: Internet
Author: User
Tags instance method sprintf

    • Use of @public, @protected, @private

When declaring a class in OC, you can use the above @public, @protected, @private Three keywords to declare the permissions of the instance, such as the following code:

    

#import<Foundation/Foundation.h>@interfaceperson:nsobject{@publicNSString*_height;@protectedNSString*_weight;@privateNSString*_wife;}@end@interfacePerson () {@publicNSString*_name; @protectedNSString*_sex; @privateNSString*_phoneno;}- (void) Publicmethod;@end@implementation Person-(instancetype) init{if(self =[Super Init]) {_height=@"1.8m"; _weight=@"65Kg"; _wife=@"None"; _name=@"Zhangsan"; _sex=@"Mans"; _phoneno=@"1122334455"; }    returnSelf ;}- (void) publicmethod{}@end

Three instances are defined in the. h file, and the permissions of these three instances correspond to public, protected, private, and P,_name are accessible anywhere for a person type of object, _sex can only be in the person

Subclass, _wife can only be accessed internally by itself. The three instances defined in the. m file also correspond to public, protected, private, and it is important to note that the object p of the person type defined in the other file cannot access the three instances, only within the. m file, public, protected , the private will play a corresponding role. In fact, the permission control of the instance in OC is only a permission limit at compile time, in the class structure of OC Runtime, there is no permission control in the instance variable, and the same class method and instance method do not have permission control.

  • For KVC, no matter what kind of permission the instance is, it can be easily accessed, as in the following code:
  •  @interfaceStudent:person@end@implementationStudent+ (void) testprotected{ person*p = [PersonNew]; NSLog (@"%@",p->_height); NSLog (@"%@",p->_weight);}-(NSString *) description{ person*p = [PersonNew]; 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]; }@end

    KVC Access:

  • #import "Other.h"#import "Person.h"@interfaceOther:nsobject- (void) testprotected;- (void) TESTKVC;@end@implementation Other- (void) testprotected{ person*p = [PersonNew]; NSLog (@"%@",p->_height);//NSLog (@ "%@", p->_weight);}- (void) testkvc{ person*p = [PersonNew]; NSLog (@"%@", [P Valueforkey:@"_name"]); NSLog (@"%@", [P Valueforkey:@"_sex"]); NSLog (@"%@", [P Valueforkey:@"_phoneno"]); NSLog (@"%@", [P Valueforkey:@"_wife"]);}@end

    The actual KVC reads an instance variable:

  • - (ID) Valueforkey: (nsstring*) key{if(!key) {        IDValue =[self valueforundefinedkey:nil]; returnvalue; }    Const Char*keycstring =[key utf8string]; Sel sel=Sel_getuid (keycstring); //Fixme:getkey, _getkey, IsKey, _iskey is missing    if([self Respondstoselector:sel]) {IDValue =[self _wrapreturnvalueforselector:sel]; returnvalue; } size_t keycstringlength=strlen (keycstring); Char*selbuffer = __builtin_alloca (Keycstringlength +5); Char*keyname = __builtin_alloca (Keycstringlength +1);    strcpy (KeyName, keycstring); #defineTry_format (FORMAT) \sprintf (selbuffer, format, keyname); Sel=Sel_getuid (Selbuffer); if([self Respondstoselector:sel]) {IDValue =[self _wrapreturnvalueforselector:sel]; returnvalue; } Try_format ("_%s"); keyname[0] = ToUpper (keyname[0]); Try_format ("is%s"); Try_format ("_is%s");//Try_format ("get%s");//Try_format ("_get%s");    #undefTry_formatif([Isa accessinstancevariablesdirectly]) {sprintf (Selbuffer,"_%s", keycstring); Sel=Sel_getuid (Selbuffer); if([self Respondstoselector:sel]) {IDValue =[self _wrapreturnvalueforselector:sel]; returnvalue; } Ivar Ivar=class_getinstancevariable (ISA, selbuffer); if(!Ivar) {Ivar=class_getinstancevariable (ISA, keycstring); }        if(Ivar) {IDValue = [Self _wrapvalue: (void*) Self +Ivar_getoffset (Ivar) Oftype:ivar_gettypeencoding (Ivar)]; returnvalue; }    }    IDValue =[self valueforundefinedkey:key]; returnvalue;}

Permissions control for instances in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.