In C # and Java, both method and variable have strict access level control, so the use of access levels in Object-c is very rare, perhaps because there is no access level syntax on method, there is nothing to control variables alone. Let's discuss the level of access to the instance variable below. When an instance variable is declared in a class, the default is protected, unlike the general C + + or C#,java default declaration, which is private. This is confusing for those of us who develop in a multi-lingual environment.
The following describes the access levels for the following instance variables:
Protection level |
complier directive |
Desciption |
Privete |
@private |
Methods declared inside a class use the |
Protected |
@protected |
Use inside this class and subclass |
Public |
@public |
To use from any class and method |
Package |
@package |
Used anywhere within the package, currently only available on 64-bit systems |
Explicit protection and implicit protection
Because protected is the default access level, you can declare it explicitly.
@interface Player:nsobject{//this is a protected instance varible that implicit declareint m_playerexperience;} @interface Player:nsobject{//this is a protected instance varible that explicit declare@prootected int m_ Playerexperience;}
There is no difference between the two ways. But the best practice is to write the access level explicitly, and immediately you know its default access level.
Access level of the OBJECTIVE-C instance variable