Ios arc mechanism and iosarc Mechanism

Source: Internet
Author: User

Ios arc mechanism and iosarc Mechanism

IOS ARC memory automatic management mechanism, currently, almost a lot of projects will use ARC, because it releases the memory to work, but although ARC is good, we still cannot leave memory management alone.

How ARC works:

ARC is a pre-compilation step. It automatically adds the retain/release/autorelease statement to our code.
ARC does not collect garbage, and the reference count does not disappear, but it becomes automatic. It sounds like an append feature. However, as long as we think about how many features of Objective-C are implemented through preprocessing of source files, we won't think so.
// The following is an example:
This is the case when we use ARC.

  NSObject *obj = [[NSObject alloc]init];    // Do any additional setup after loading the view, typically from a nib.

This is true with ARC.

 NSObject *obj = [[NSObject alloc]init]; [obj release];

Here are some official introductions
After the emergence of ARC, some new rules emerged: (source)
1. The method for creating an object using Alloc/Init is the same as before, but you cannot call retain/release/autorelease/retainCount. You cannot use the selector to secretly call them: Do not use @ selector (retain) or @ selector (release ).
2. dealloc Method
If ARC is automatically called for you, you must not directly call dealloc. However, if you need to release resources other than instance variables, you can still create a custom dealloc method. In this method, do not call [super dealloc]. Because ARC will help you tune it.
3. Declared attributes
Before ARC, we used the assign/retain/copy parameter in the @ property command to tell the compiler how to manage the memory of these attributes. After ARC is used, these parameters are voided and the weak/strong parameters are used.
4. object pointer in the C structure
It is also forbidden to use. We recommend that you do not put them in the structure and change them to the class. Otherwise, the ARC will not recognize them. Some porting problems may occur. However, ARC can be closed in files. Refer to "introduce codes that are incompatible with ARC" below ".
5. Replace @ autoreleasepool with the NSAID utoreleasepool
ARC-compatible Code cannot use the NSAID utoreleasepool object. Instead, use the @ autoreleasepool {} block instead. A good example:

int main(int argc, char *argv[]){  @autoreleasepool {    return UIApplicationMain(argc, argv, nil, NSStringFromClass([ExampleAppDelegate class]));  }}

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.