Day Fourth: Memory management-property Parameters

Source: Internet
Author: User

1. There are two ways to refer to a class generally:

1 , through # Import Method introduces

2. Introducing by @class

#import and the @class The difference:

#import "B.h" @interface a:nsobject {B *b;} @end

1.#ImportThe method contains all the information for the referenced class, including the variables and methods of the referenced class. @classthe only way to tell the compiler in the A.h fileB *bjust the declaration of the class, specifically what the information in this class, it is not necessary to know, and so on the implementation of the file is really used, will go to seeBthe information in the class.

2.Use@classmethod because only the referenced class is known (BClass), and in the real class because of the implementation variables and methods used in the referenced class, the. Mthe file needs to use#Importto include the header file of the referenced class.

3. If some files are #importonce, then once the initial header file changes slightly, then all the classes referenced to this file need to be re-compiled again, such efficiency is conceivable. In contrast, the use of the @class usually does not occur.

4. Memory Management _property parameters:

(when a member variable in a class has a number of OC The question of whether an object is to manage memory between objects is cumbersome, especially in setter methods, when the compiler introduces the property's retain parameter to manage memory.

1.retain: If the property has a retain parameter, the compiler will automatically add it to the setter method, firstRelease old value, and thenretain the new value.  So, if you are defining a basic data type in a class such as:@ Propertyint Age . No action is required at this time, which already contains the assign operation (directly assigned value),But if the class is defined as aOCobject, then you need to use retain such as:@property (retain) Student *stu;

Assign : Default Type , the setter method is assigned directly and does not perform retain operation.

Readonly : Method to generate getter only

Readwirte : setter and getter methods are generated

Copy : Setter method to release the old value, and then copy the new value.

Atomic : Default Properties : Provides multithreading security. , which means lock the method to ensure thread safety (consumption performance)

Nonatomic, the delegate method does not need to consider thread safety issues.

Note: Basically all of the properties are nonatomic types, and there is no need to consider atomic because of performance issues.

property can also refer to Fixed setter and the name of the Getter method, for example: @ property (setter =  setage,getter =  getage ) int age;  This specifies setter and getter method:

5. automatic release of the pool:

is a OC inside a memory automatic recovery mechanism, you can generally put some temporary variables in the automatic release pool, unified recovery release

When the auto-free pool is destroyed, all objects inside the pool call the release method once:

OC object only needs to send a autorelease Message, the object is added to the nearest auto-release pool (the release pool at the top of the stack)

Autorelease actually just delayed the call to release, for every autorelease, The system simply puts the object in the current autoreleasepool , and when the pool is released, all objects in the pool are called release.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fourth day: Memory management-property Parameters

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.