Objective-c @ property and @ synthesize

Source: Internet
Author: User

In objective-C, @ property is used to identify attributes (generally instance variables ). Use @ synthesize in the implementation file to identify the declared variables, so that the system can automatically generate the setting and obtaining methods.

That is to say, the @ property and @ synthesize pairs allow the system to automatically generate the setting and obtaining methods.

Example:

Test. h

 
# Import <Foundation/Foundation. h> @ Interface Test: nsobject {int intx; int inty ;}@ property int intx, inty;-(void) print; @ end

Test. m

 
# Import "test. H "@ implementation test @ synthesize intx, inty;-(void) print {nslog (@" intx + inty = % d ", intx + inty);} @ end

Classtest. m

# Import <Foundation/Foundation. h> # import "test. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; test * test = [[test alloc] init]; [Test setintx: 1]; [test setinty: 1]; [test print]; [test release]; [pool drain]; return 0 ;}

ProgramRunning result:
Intx + inty = 2

Obviously, there are no methods in the test class definition and class implementation: setintx and setinty, but we can use these two methods to set values for the instance variables of the class during program writing, this is the result of @ property and @ synthesize.
This is the simplest use of @ property and @ synthesize.

In addition ". "Operation (vertex operator), using this operator can easily access attributes (or member variables in the vertex class can be very convenient, a bit like the taste of C/C ++ ), because, the preceding settings can also be written in the following form:

 
Test. intx = 1; test. inty = 1;

It is equivalent:

 
[Test setintx: 1]; [test setinty: 1];

Note the following:"." Operations (vertex operators) can only be used in setter and getter methods, but cannot be used in other methods of the class.
(Most of the books use vertex operators for introduction. The principle is here)

@ Property has many other attributes. @ Property and synthesize enable the system to automatically generate the setting method and obtain the method. What is the method automatically generated by the system? What are the rules? Next we will introduce it.

Related Article

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.