Please indicate the article link at the beginning of the Post. Please support originality.
I have been wondering whether there are two ways to declare variables in objective_c. Today I am free to discuss it with netizens and I have learned a little about it after I checked stackoverflew. The record is as follows:
An OC is used, and two variables are defined.
1. In the brackets @ interface: nsobject {}, of course, nsobject refers to a parent class and can be other.
The format is as follows:
1 @interface GCTurnBasedMatchHelper : NSObject {2 BOOL gameCenterAvailable;3 BOOL userAuthenticated;4 }
2. Another method is to define a variable directly after @ interface: nsobject.
1 @property (assign, readonly) BOOL gameCenterAvailable;
You will find that someone will define the same variable again in @ interface after defining the variable, which is very common.
The result may be as follows:
1 @interface GCTurnBasedMatchHelper : NSObject {2 BOOL gameCenterAvailable;3 BOOL userAuthenticated;4 }5 6 @property (assign, readonly) BOOL gameCenterAvailable;
In addition, you can define variables in @ interface instead of @ property. You can also define variables only by @ property instead of @ interface. Of course, @ property is used for definition, generally. in the M file, @ synthsize is used to synthesize the corresponding setter and getter methods. Otherwise, a warning is returned. Of course, @ synthsize is optional, but it is recommended by Apple. No consequence is required. I have never tried it. You can try it if you are interested.
What is the difference between the two methods.
1. if you only define variables in @ interface, the variables you define can only be accessed in the current class, but cannot be accessed in other classes; variables declared with @ property can be accessed externally.
2. variables declared using @ property can be read and written using the "self. variable name" method. The @ interface method is not supported.
3. Here is a link: http://stackoverflow.com/questions/9702258/difference-between-properties-and-variables-in-ios-header-file
Here we will talk about my English dishes:
Defining the variables in the brackets simply declares them instance variables.
Defining a variable in parentheses simply declares an instance variable (the instance variable should be a member variable ). Blogger Note: foreigners have different understandings of variable and instance variable. Therefore, a fuzzy word Ivar is used below.
Declaring (and synthesizing) a property generates getters and setters for the instance variable, according to the criteria within the parenthesis. This is particle-ly important in objective-C
Because it is often by way of getters and setters that memory is managed (e.g ., when a value is assigned to an Ivar, it is by way of the setter that the object assigned is retained and ultimately released ). beyond a memory management strategy, the practice
Also promotes encapsulation and reduces the amount of trivial code that wowould otherwise be required.
A declaration (and @ synthsize) attribute generates getter and setter methods for member variables. According to the standards in brackets, it is very important to use setter and getter in OC for memory management. (For example, when a value is assigned to this variable, the object is allocated through the setter function, the counter is modified, and finally released ). At a higher level, this approach also promotes encapsulation and reduces unnecessary code.
It is very common to declare an Ivar in brackets and then an associated property (as in your example), but that isn't strictly necessary. defining the property and synthesizing is all that's required,
Because synthesizing the property implicitly also creates an Ivar.
It is common to define a variable in @ interface brackets and repeat it with @ property. In fact, it is not necessary. It is enough to use @ property and @ synthszie, because a variable is created when @ synthsize is used to synthesize the read/write method of this attribute.
The approach currently suggested by Apple (in templates) is:
Currently, the recommended method for Apple (in the template) is as follows:
-Define property in header file, e.g .:
First define an attribute in the header file
1 @property int gameCenter;
Then synthesize & declare Ivar in implementation:
Then synthsize and declare in the implementation file are as follows:
1 @synthesize gameCenter = __ gameCenter;
The last line synthesizes the gamecenter property and asserts that whatever value is assigned to the property will be stored in the _ gamecenter Ivar. Again, this isn' t necessary, but by defining
The Ivar next to the synthesizer, you are switching the locations where you have to type the name of the Ivar while still explicitly naming it.
The last row of synthsize gamecenter indicates that no matter what value is assigned to this attribute, it will be stored in the _ gamecenter variable. Again, this is not necessary. However, after writing this code, you can reduce the number of variable names that have been explicitly named.
The last sentence means you are loading the locations where you have to type the name of the Ivar while still explicitly naming it.
According to Qian Feng's 2nd grammar course, the internal getter method of @ synthsize can be expanded
1 -(int)gameCenter2 {3 return _gameCenter;4 }
Directly write @ synthsize gamecenter;
The setter function is expanded
1 -(int)gameCenter2 {3 return gameCenter;4 }
Note: The function name and variable name are the same. In his Stanford course, Professor Bai beam also blurted that such a name may bring bugs. He didn't say anything about the specific bug, and I have never seen it, therefore, it is better to develop this writing habit. Getter functions in other languages generally add get before variables, but OC does not. It may be used to distinguish it from other languages. It is a feature of OC, but the result is so troublesome.