OC learning --- use of @ property and @ synthesize, ocsynthesize

Source: Internet
Author: User

OC learning --- use of @ property and @ synthesize, ocsynthesize

In the previous article, we introduced memory management in OC: Memory:@ Property and @ synthesize


I. @ property keywords

This keyword is used to quickly define an attribute in OC, and it can set some values to achieve a certain effect, such as the reference count problem.

Let's take a look at his usage:

/// Person. h // 25_Property // Created by jiangwei on 14-10-12. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> @ interface User: NSObject {// NSString * _ userName; // NSString * _ passWord ;//...} // The first step is to generate the _ userName attribute // The second step is to automatically generate the set/get method for the _ userName attribute. // is the property referenced in the generated set method? // Three methods of the set Method: // The first method: // normal value assignment // reference operation of the general object type // reference operation of the NSString object type // The first position // atomic: thread-protected, default // nonatomic: the thread is not protected // The second position // assign: direct value assignment, default // retain: Keep object // copy: copy object // The third position // readwrite: generate the get/set method. Default Value: // readonly: generate only the get method.
@property NSString *userName;

@ End

Remember that when we previously defined the attribute {...} and the get/set method may need to be implemented after the definition is complete. Here we use the @ property keyword for definition:

@property NSString *userName;
After the definition, we can use this attribute:

After this method is defined, the set/get method is automatically available for this attribute.

Step 1: generate the _ userName attribute

Step 2: automatically generate the set/get method for the _ userName attribute

Is this definition much more convenient than before?


Next, let's take a look at three other values that can be set:

@property(atomic,retain,readwrite) Dog *dog;

1. value at the first position:

Atomic: thread-protected, default

Nonatomic: The thread is not protected.

2. the value at the second position:

Assign: direct value assignment, default

Retain: indicates the object to be retained. The retain method is automatically called internally and the reference count is + 1.

Copy: copy object

3. value at the third position:

Readwrite: generate the get/set method. The default value is

Readonly: only get methods are generated.


Here is an example:

Main. m

//// Main. m // 25_Property // Created by jiangwei on 14-10-12. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "User. h "# import" Dog. h "// when a class has many attributes, We need to manually write their set/get method //, which is time-consuming, so now you can use @ propertyint main (int argc, const char * argv []) {User * user = [[User alloc] init]; dog * dog = [[Dog alloc] init]; NSLog (@ "count: % ld", [dog retainCount]); [user setDog: dog]; NSLog (@ "count: % ld", [dog retainCount]); return 0 ;}
Running result:




2. @ synthesize keyword

/// Person. m // 25_Property // Created by jiangwei on 14-10-12. // Copyright (c) 2014 jiangwei. all rights reserved. // # import <Foundation/Foundation. h> # import "User. h "// sometimes we don't want to define attributes starting with _ // at this time we can use @ synthesize, to modify the desired attribute name // at this time, the attribute _ userName becomes $ userName @ implementation User @ synthesize userName = $ userName; @ end
Because after @ property is used to define an attribute, if we want to modify the attribute name, we can use the @ synthesize keyword to modify the attribute name.

@synthesize userName = $userName;


Summary

This article mainly introduces the use of two keywords, @ property and @ synthesize, especially the @ property keyword. It is almost used for defining attributes later, which is very convenient.







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.