Four access controllers of member variables in objective-C:
@ PRIVATE: only the current class can be accessed internally.
@ Public: All users can access
@ Protected: only the current class and its subclass can be accessed.
@ Package: allow member variables controlled by the package to be accessed not only in the current class, but also in other programs with the same image.
What is "same image "?
Is the same framework or the same execution file generated after compilation.
That is, the current framework can be accessed, but external programs cannot. (If it is public, external programs can also access it ).
|
@ Private |
@ Package |
@ Protected |
@ Public |
Same type |
??? |
??? |
??? |
??? |
In the same image |
|
??? |
|
??? |
Subclass |
|
|
??? |
??? |
Within the global scope |
|
|
|
??? |
Example:
Interface Section: (fkapple. h)
# Import <Foundation/Foundation. h>
@ Interface fkapple: nsobject
{
// Use @ package to restrict member variables
@ Package
Double _ weight;
}
@ End
Main Section:
# Import "fkapple. H"
Int main (INT argc, char * argv [])
{
@ Autoreleasepool {
Fkapple * Apple = [[fkapple alloc] init];
Apple-& gt; _ Weight = 30.4;
}
}
Objective-C Access Controller