Look at the code first.
Copy Code
- Appdelegate.h
- @property (weak) Iboutlet Nsmatrix *stocktype;
- @property (weak) Iboutlet Nsmatrix *market;
- Appdelegate.m
- Nscell *st=[market SelectedCell];
|
Compile, always prompt, can not find the market variable, but Stocktype is not a problem.
If the system suggests that the line is underlined before the market, it becomes _market and can be compiled and executed normally.
But what is it for?
In contrast, I find that there is another section in M files.
Copy Code
- Appdelegate.m
- @synthesize Stocktype;
|
The difference is here, only after synthesize with the market, there is no need to underline.
Although the reasons are unclear, at least the problem is solved.
A:
The compilation time of Xcode is caused by automatic completion
In the. m implementation file, if you use the property, you must call the Getter method using Self.property, and if you want to directly instantiate the variable, you must synchronize it with the synthesize keyword in the M file.
So in early Xcode (which I remember should be before 4), in. m files, all properties must be synchronized by handwriting @synthesize property name = Instance variable name
In accordance with the official OC naming convention, in order to avoid variable leakage, the instance variable name is generally recommended to use the underscore prefix notation, that is, if the property name is called ABC, the corresponding instance variable name is defined as _ABC
Therefore, all the property in the. m code needs to be manually synchronized with the instance variable in the way @synthesize property = _property
This kind of writing is so common, so that after Xcode4, the editor added automatic synchronization completion function, only need to define the property in H file, in the compilation period m file will automatically fill out @synthesize property = _property Code, no longer need handwriting, Avoids manual coding of "Physical Code"
However, this requires that the instance variable name must be equal to "_" + attribute name is not very flexible, if the developer needs to specify a different property name, you need to manually write the @synthesize in the. m file
In your case,
If I don't write anything in M,
Xcode defaults to the market property at compile time, complete with @synthesize market = _market, and the instance variable named _market
if @synthesize market is specified in M
Xcode will assume that you have manually specified the instance variable named market and the compilation period is complete: @synthesize market = Market, the instance variable is named market
It is easy to verify that you defined in M as @synthesize market = _xyz, and the instance variable name you used in the. m file is _xyz.
iOS underline variable: Why is it useful to underline before a variable?