The properties of OC Learning notes are easy to understand and error-prone.

Source: Internet
Author: User

The concept of a property is present in OC1.0, in the form of defining an instance variable, then defining the setter and Getter methods, manipulating the properties with the dot operator

For example, the interface part of a class

1 @interfaceFather:nsobject2 {3 Nsinteger _item;4 }5 #pragmaMark-Properties6-(void) SetItem: (Nsinteger) item;7-(Nsinteger) item;8 9-(void) Setaaa: (Nsinteger) AAA;Ten-(Nsinteger) AAA; One  A @end

Implementation part of the class

1 #import "Father.h"2 3 @implementationFather4 5-(void) SetItem: (Nsinteger) item{6_item =item;7 }8-(Nsinteger) item{9     return_item;Ten } One  A-(void) Setaaa: (Nsinteger) aaa{ -_item =AAA; - } the-(Nsinteger) aaa{ -     return_item; - } -  + @end

Main function

 1  Father *test = [[Father Alloc]init];2  Test.item = ; 3  NSLog (@ " %ld   ,test.item);  4  5  test. AAA = 20   6  NSLog (@ " %ld  , Test. AAA); 

In the example above, an instance variable and two attributes are defined, and two properties operate on the same instance variable, noting that the property and instance variables are not necessarily associated, and that the property is simply manipulating instance variables (not even manipulating instance variables, which are explained in detail later), but the naming rules for setter and getter methods need to be noted , the setter method is named set+ property name, getter named method is the property name (property AAA Setter method is the Setaaa:,getter method name is AAA)

OC2.0 later provides @property and @synthesize keywords to simplify the writing of attributes from the syntax layer

Declaration of a property

1 #import<Foundation/Foundation.h>2 3 @interfaceFather:nsobject4 {5 Nsinteger _item;6NSString *_str;7 }8 #pragmaMark-Properties9 @property Nsinteger Item;Ten@property NSString *AAA; One  A @end
View Code

Implementation of attributes

1 @implementation Father 2 3 @synthesize item = _ITEM,AAA = _str; 4 5 @end
View Code

It should be noted at this point that two different properties cannot manipulate the same instance variable at the same time, and the syntax check will error .

After Xcode5, the code for the attribute implementation section can be omitted, and Xcode generates the code for the property implementation (Apple's official recommended notation)

There are a bit more points to be aware of here, each analysis.

After omitting the attribute implementation part of the code, Xcode gives the default property implementation code like this

@synthesize Property 1 = _ Property 1, Property 2 = _ Property 2;

The _+ property name is the instance variable of the attribute operation, as the example of the above attribute declaration code, the code generated by Xcode is like this

@synthesize item = _ITEM,AAA = _AAA;

Note that only _item,_aaa in our class interface file is not, and this variable is not, He is a true. Private instance variable that is automatically generated by Xcode, because this variable is not generated in the interface file, it is declared in the implementation file of the class, and is declared at the top of the class, roughly like this

1 @implementation Father 2 3 NSString *_aaa; 4 5 @synthesize item = _ITEM,AAA = _aaa; 6 7 @end
View Code

Therefore, when father as a parent class, _AAA is not inherited as an instance variable under @private, so it is not recommended that you declare and attribute the same name in the parent class in the subclass, especially if there are no corresponding instance variables, for reasons you can consider.

The properties of OC Learning notes are easy to understand and error-prone.

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.