iOS underline variable: Why is it useful to underline before a variable?

Source: Internet
Author: User
Tags naming convention

Look at the code first.

Copy Code
  1. Appdelegate.h
  2. @property (weak) Iboutlet Nsmatrix *stocktype;
  3. @property (weak) Iboutlet Nsmatrix *market;
  4. Appdelegate.m
  5. 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
  1. Appdelegate.m
  2. @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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.