Notes: Some Understandings of objective-c Property

Source: Internet
Author: User

Declare in the header file:

@property (nonatomic,strong) NSString * str;

In oC, this line of code indicates a property named Str.

Declare in the implementation file. M:

@synthesize str;

When the class in OC declares this sentence, two methods (getter and setter of the property generation) will be automatically generated, one attribute. If the property is implemented (in. in the M file), an instance variable is generated.

If attributes are declared in the category, two methods are automatically generated. setter and getter must be implemented. attribute in the category must use @ dynamic, but instance variables are not generated.

In class expansion, the property must be implemented in the. M file of the class, and it does not contain setter, Getter, and instance variables.

The attribute declared in the Protocol must be implemented in the. M file of the class that complies with the interface. Otherwise, a warning will be prompted and no setter, Getter, and instance variable will be generated.

Summary: in the class, the attribute can be implemented automatically without being written. However, in the class, the attributes declared in the class extension and protocol must be implemented, and the attribute does not take effect, because the system does not automatically generate getter and setter. At the same time, class extension can add private variables and private methods for classes. classes can only add methods for classes, but cannot add anything else. The ultimate objective of the attribute is to: method, instance variable.

(This is a summary of O (operator _ operator) O Haha ~)

 

1. Differences between assign, retain, and copy:

1 NSString * newstr=[str assign];2 NSString * newstr=[str retain];3 NSString * newstr=[str copy];

Original STR address: 0x1111 content: 0x2222, storage content address: 0x2222, content: @ "ABC"

Variable symbol address content

Assign newstr 0x1111 0x2222 0x2222 @ "ABC"

Retain newstr 0 xaaaa 0x2222 0x2222 @ "ABC"

Copy newstr 0 xcccc 0x3333 0x3333 @ "ABC"

Asssign is equivalent to pointer assignment, and does not operate on the reference count. Note that the original object is no longer used and must be set to nil.
Retain is equivalent to adding 1 to the reference count of the original object
Copy does not change the reference count of the original object. The reference count of a new object is 1.

This is the difference between assign, retain, and copy in memory.

 

2. atomicity

Atomicity, nonatomic (default atomicity, single thread)

Whether it is a single-threaded operation.

Readable and writeable: readwirte and readonly (read/write by default)

 

3. strong and weak (from others' articles ^_^)

The strong and weak keywords are added to attribute settings in iOS 5 to modify attributes (ARC is not supported before iOS 5)

Strong is used to modify attributes of strong references;

@ Property (strong) someclass * aobject;

Corresponds to the original
@ Property (retain) someclass * aobject; and @ property (copy) someclass * aobject;

Weak is used to modify attributes of weak references;
@ Property (weak) someclass * aobject;
Corresponds to the original

@ Property (assign) someclass * aobject;

 

Write so much first, and wait for the study to continue ..

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.