This is the record on the document.
The Scope of Instance Variables
To enforce the ability of a object to hide it data, the compiler limits the scope of instance variables-that is, limits Their visibility within the program.
To force an object to hide its data, the compiler restricts the scope of the instance variable to limit its visibility in the program
But to provide flexibility, it also lets you explicitly set the scope at four levels. Marked by a compiler directive:
But to provide flexibility, Apple also allows developers to explicitly set the range (four choices)
Directive |
Meaning |
@private |
The instance variable is accessible only within the class, that declares it. |
@protected |
An instance variable can be accessed only by the class that declares it The instance variable is accessible within the class, the declares it and within classes that inherit it. All instance variables without an explicit scope directive has @protected scope. An instance variable can be declared to be accessed by its classes and subclasses, and all instance variables that do not explicitly define the scope are @protected |
@public |
The instance variable is accessible everywhere. Instance variables can be accessed from anywhere. |
@package |
Using the modern runtime, an @package instance variable have @public scope inside the executable image that implements the class, but acts like @privateoutside. When running with modern, a @package instance variable is actually @public in the implementation of this class's executable image, but outside is @private " Runtime need to look at the Apple Document Runtime Programming Guide The @package scope for OBJECTIVE-C instance variables are analogous toprivate_extern for C variables and functions. Any code outside the class implementation ' s image, tries to use the instance variable gets a link error. The @package in Objective-c is similar to the private_extern of variables and functions in the C language. Any code that is outside the mirroring of the implementation class that wants to use this instance variable will cause link error This is the most useful-instance variables in the framework classes, [email protected] may was too restrictive but @prote CTED or @public too permissive. This type is most commonly used for instance variables of framework classes, using @private too restrictive, using @protected or @public and too open |
Objective-c settings for access rights. is also the scope of the variable.
Protected-methods defined in the class and any subclasses can directly access to the instance variables that follow. This is the default case. The method in this class and all subclasses can directly access such a variable, which is the default.
Private-methods defined in the class can directly access the instance variables that follow, but subclasses cannot. A method in this class can access such a variable, and the subclass cannot.
Public-methods defined in the class and any other classes or modules can di-rectly access the instance variables that fo Llow. In addition to the methods in your own and subclasses, you can also be accessed by methods in other classes or in other modules. Openness is greatest.
Package-for 64-bit images, the instance variable can be accessed anywhere within the image that implements the class. For 64-bit images, such member variables can be accessed arbitrarily in the image that implements the class.
After study, @package variable, for the framework interior, equivalent to @protected, for the outside of the framework, equivalent to @privat.
This feature is ideal for developing a static class library for third parties, because most people don't want to let others know the value of their property.
Write a little bit of code to test this scope:
@interface Testclass:nsobject {
@private
NSString *mnameprivate;
@package
NSString *mtextpackage;
}
-(void) print;
@end
@implementation TestClass
-(ID) init
{
Self=[super Init];
if (self) {
Mnameprivate = [[NSString alloc] initwithutf8string: "Mnameprivate"];
Mtextpackage = [[NSString alloc] initwithutf8string: "Mtextprivate"];
}
return self;
}
-(void) print
{
NSLog (@ "private:%@, package:%@", Mnameprivate, Mtextpackage);
}
-(void) dealloc
{
[Mnameprivate release];
[Mtextpackage release];
[Super Dealloc];
}
It is possible to access the member variables of the package scope within other classes, which are required to use the access method.
TestClass *t = [[TestClass alloc] init];
[t print];
NSLog (@ "%@", t->mtextpackage);//ok
NSLog (@ "%@", t->mnameprivate);//Compilation error, prompting "instance variable ' mnameprivate ' is private"
NSLog (@ "%@", [t mnameprivate]);//compile Prompt "mnameprivate not found," program can run, but error
[t release];
The range type of four instance variables in iOS @[email protected]@[email protected]