"Original" IOS rewrite both setter and getter when error: Use of undeclared identifier ' _name ';d ID of ' mean ' name '

Source: Internet
Author: User

Write so many code, usually also did not pay attention to this error, because usually very rarely at the same time rewrite setter and getter method, generally speaking, we are probably using lazy loading method, and then rewrite the Getter method, do a non-empty judgment. Then sometimes, depending on the requirements, you want to override both the setter and getter methods of the property. The system will report an error:: Use of undeclared identifier ' _name ';d ID you mean ' name '

MARK: If you don't want to know too much, a straightforward workaround:

Adding a line of code to the @implementation implementation is OK

@synthesize wtname = _wtname;

To explain:

OC initially sets the role of @property and @synthesize:

The role of @property is to define properties and declare Getter,setter methods. (Note: property is not a variable)
The role of @synthesize is to implement attributes, such as the Getter,setter method.

In the case of declaring a property, if you override the Setter,getter method, you need to define the unrecognized variable in @synthesize and use the accessor method of the property for the variable. Such as:

The. h file

Later, because the use of @property ash often, the expression of @synthesize is abbreviated.

From Xcode4.4 after @property has been the monopoly of @synthesize function mainly has three functions:

(1) A private underlined member variable is generated so the subclass cannot be accessed directly, but can be accessed through the Get/set method. So if you want the defined member variable to have the subclass direct access then you can only define the member variable in the. h file, because it defaults to @protected
(2) The implementation of the Get/set method is generated.
When:
The member property declared with @property is equivalent to the auto-generated setter getter method, and if the set and get methods are overridden, the member property declared with @property is not a member property, it is another instance variable, and this instance variable needs to be declared manually. So the error will be reported.
Summary: Be sure to distinguish between attributes and variables, not confused. @synthesize the Declared property = variable. This means that the Setter,getter method of the property acts on this variable.

"Original" IOS rewrite both setter and getter when error: Use of undeclared identifier ' _name ';d ID of ' mean ' name '

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.