OBJECTIVE-C (9) Arc of memory management

Source: Internet
Author: User

    • ARC mechanism and judging criteria

Arc:auto Reference counting Automatic reference count

is a compiler mechanism to add retain, release, Autorelease and other methods to our code during the compilation process.


Forced to use after iOS7.0, so that programmers no longer care about memory management, requirements:

1) cannot use retain release and other methods of reference counting

2) You cannot use the Autorelease method, but you can use the auto-release pool

3) Override the Dealloc method to no longer use [super Dealloc]


Judging criteria:

Object is freed as long as no strong pointer is pointed to the object

Strong pointer:__strong (default)

Weak pointer:__weak


Do not appear with this code:

__weak person *p = [[Person alloc] init];


Method Autorelease is not allowed under the ARC mechanism, but is not discarded @autoreleasepool

ARC is simply a code call to help you join retain/release/autorelease at compile time.

Therefore, the use of @autoreleasepool under arc is still valid, ensuring that objects in the pool are delayed release.


    • @property Memory Management parameters

@property Memory Management parameter strong represents a strong pointer property, weak the table weakness pointer parameter.

Simple memory: When the property is OC object

Use strong instead of retain

Use weak instead of assign. Using the weak parameter to resolve circular reference problems

Mrc:

Retain:release property old value, retain property new value

Assign: Direct assignment without changing the reference count (default)

Generally used for non-OC objects, and without the possession of objects

Copy:release old value, copy new value, commonly used for nsstring

ARC:

Strong: Strong pointer, equivalent to retain, for object

Weak: weak pointer, for object

Assign: For non-OC objects

Copy: For NSString



    • MRC and Arc

To modify the MRC project to Arc:

Xcode edit-"refactor-" Convert to OC ARC

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7C/7D/wKioL1bRlSDRzYk-AAKtJ_B3ndw904.png "title=" screen shot 2016-02-27 pm 8.20.22.png "width=" 601 "height=" 364 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:601PX;HEIGHT:364PX; " alt= "Wkiol1brlsdrzyk-aaktj_b3ndw904.png"/>

Let some. m files in the ARC project do not use arc

Project configuration-"Build phases-" Compile sources

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/7F/wKiom1bRlRvxb9p2AAKIAaRkTDI910.png "title=" screen shot 2016-02-27 pm 8.22.17.png "width=" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:270px " alt= "Wkiom1brlrvxb9p2aakiaarktdi910.png"/>

Sets the compiler flags:-fno-objc-arc for the specified. m file

Use arc for some. m files in non-ARC projects:

Compiler Flags:-f-objc-arc


This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745690

OBJECTIVE-C (9) Arc of memory management

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.