Objective-c Property Learning

Source: Internet
Author: User

Objective-C 2.0Medium,

In the interface file, that is, afterThe suffix name is. HFile, use@ PropertyComeIdentifierAttribute (generallyInstance variables);

InImplementationFileIn the. M file, @ synthesize is used to identify the declared attribute, so that the system will automatically generate the setter and getter methods.

The following is an example.Program:

# Import <Cocoa/cocoa. h> @ interface useproperty: nsobject {int intx; int inty ;}@ property int intx, inty; @ end @ implementation useproperty @ synthesize intx, inty;-(void) print {nslog (@ "sum of the two numbers: % I", intx + inty);} @ endint main (INT argc, const char * argv []) {NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; useproperty * testproperty = [[useproperty alloc] init]; [testproperty setintx: 2]; [testproperty setinty: 3]; [testproperty print]; [pool drain]; return 0 ;}

Note: In the preceding example, we have not defined the setintx and setinty methods, but can use them. This is the benefit of using property. Is it better than writing setter, the getter method is much more convenient... xi

Common attribute values in the attribute list and their meanings:

AttributeDescription

Assign   Use a simple value assignment statement to set values for instance variables

Copy   Use the copy method to set the value of instance variables

Noneatomic Return Value directly. If this attribute is not declared, it is the atomic attribute, and the storage of the instance variables is mutually exclusive. In an environment without garbage collection, the system retain the instance variable and sets

Autorelease Then return value

Readonly The instance variable value cannot be set. The Compiler does not generate the setter method.

Readwrite You can obtain and set instance variable values. In the implementation class file, use @ synthesize to automatically generate the setter and getter methods by the compiler. This is the case in the above example.

Retini Execute the retain operation when assigning values.

Getter = Name The setter method uses the name specified by name instead of the name of the instance variable.

Setter = Name The getter method uses the name specified by name instead of the name of the instance variable.

It's so sleepy. I have to wash and sleep. I will continue to learn the basic syntax of objective-C tomorrow.

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.