About the property in objective-C

Source: Internet
Author: User

PropertyProbablyObjcUnique element inProgramming Mechanism, used to quickly generate class data membersGetAndSetMethod. This articleAbout mePropertySome usage experience.

1) Retain, copy, and assign:

Retain is used to generate the reference count + 1 of the data member (which must be an objc object) in the set method. It is usually used for objc objects other than the nsstring type. For example

-(Void) setmember :( member *) member <br/>{< br/> [member _ release]; <br/> member _ = [member retain]; <br/>}
A common exception is that the Set Method of delegate is usually set to assign, which is used to avoid the reference count caused by circular references not returning to 0, memory leakage-consider the situation where the delegate of a view is the controller of the view.

The Set Method of the nsstring class object is set to copy, and the string object needs to be copied in depth.

The Set Method of the variable should be set to assign.

2) elegantly release Member objects.

Taking controller as an example, the member object needs to be released in the viewdidunload method and dealloc method. The direct method is

[Member _ release];

The problem with this is that a wild pointer is generatedProgramUnexpected bugs may occur. If the program design is odd,

If you are lucky enough.

The elegant method is

Self. Member = nil;

Expand

[Member _ release]; <br/> member _ = nil;
3) nonatomic and atomic

Atomic is the meaning of atoms. In a multi-threaded program, the data members of atomic are locked during reading and writing, ensuring data security with a very small extra cost. Nonatomic assumes that the data member is not read and written by multiple threads at the same time.

My opinion-it is unrealistic to accurately determine whether a data member can be read and written by multiple threads at the same time in actual programming. Therefore, nonatomic is not required, and the additional overhead caused by this is negligible.

Misuse of nonatomic may cause unexpected bugs-if you are lucky enough.

4) private and public

We should try to set property to private-this will make the Class header file look much more concise. Generally, the property of almost all data members in a controller can be set to private -- otherwise, the program design may be faulty.

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.