iOS beginner, about variables and underline the problem

Source: Internet
Author: User

Why do iOS developers need to underline before variables are used?

After seeing this man's explanation, finally understand, reprint to this. Link here: http://www.cocoachina.com/bbs/read.php?tid=234290

In the. m implementation file, if the property is used, the getter method must be called using Self.property. By the way, actually Self.property is actually called the Getter method.

OC syntax Description of the point expression:"dot expression (.) It looks a bit like the access to the struct in C and the Java language Summary of object access, which is intended by OC designers. If a point expression appears on the equals sign = left, the setter method for the property name is called. If the point expression appears on the right, the getter method for the property name is called. "

Or

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 beginner, about variables and underline the problem

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.