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 @private outside. When using the modern runtime, 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 to Private_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, where @private may was too restrictive but @protecte D 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 |
The range type of four instance variables in OC @[email Protected] @[email Protected]