Objective-c Grammar Quick over (7)

Source: Internet
Author: User

ARC (is compiler feature)

    • ARC is a new feature that has been added since iOS 5, completely eliminating the cumbersome manual management of memory, and the compiler will automatically insert the appropriate retain, release, and autorelease statements where appropriate. You no longer need to worry about memory management, because the compiler handles everything for you
    • ARC is a compiler attribute, not an IOS runtime feature, nor is it similar to a garbage collector in other languages. So ARC and manual memory management performance is the same, and sometimes faster, because the compiler can also perform some optimizations
Arc Fundamentals (not Java-like garbage collection mechanism)the rules of ARC are very simple: as long as there is a strong pointer variable pointing to the object, the object remains in memory

Strong pointers and weak pointers

    • Default all instance variables and local variables are strong pointers
    • The weak pointer automatically becomes a nil pointer when the object pointed by the weak pointer is reclaimed, and no wild pointer error is thrown

ARC judgment: As long as there is no strong pointer to the object, it will release the object, weak pointers do not, in a timely manner a weak pointer to the object, the object does not have a strong pointer, it will be automatically released. In general, you do not need to explicitly declare a strong pointer, but in the encapsulation, you need to specify when you define the method. A weak pointer, however, must be explicitly stated. The default is strong pointers.

ARC Features

1> does not allow calls to release, retain, Retaincount

2> allows rewriting of dealloc, but does not allow calls to [super Dealloc]

Parameters of the 3> @property

* Strong: Member variable is strong pointer (for OC object type)

* Weak: member variable is weak pointer (for OC object type)

* Assign: For non-OC object types

4> former retain changed to strong

OC pointers are divided into 2 types:

1> Strong pointer: By default, all pointers are strong pointers __strong

2> Weak pointer: __weak

/*file name: Dog.h*/#import<Foundation/Foundation.h>@interfaceDog:nsobject@end/*file name: dog.m*/#import "Dog.h"@implementationDog- (void) dealloc{NSLog (@"Dog is Dealloc");}@end/*file name: Person.h*/#import<Foundation/Foundation.h>@classDog;@interfacePerson:nsobject@property (nonatomic, strong) Dog*Dog; @property (nonatomic, strong) NSString*Name: @property (nonatomic, assign)intAge ;@end/*file name: person.m*/#import "Person.h"@implementation Person- (void) dealloc{NSLog (@"Person is Dealloc"); //[Super Dealloc]; can not write, otherwise error}@end//main.m#import<Foundation/Foundation.h>#import "Person.h"#import "Dog.h"intMain () {Dog*d =[[Dog alloc] init]; person*p =[[Person alloc] init]; P.dog=D; D=Nil; NSLog (@"%@", P.dog); return 0;}voidTest () {//wrong way of writing (meaningless notation)__weak person *p =[[Person alloc] init]; NSLog (@"%@", p); NSLog (@"------------");}

Refactoring Old Code (manual memory management refactoring to ARC mode) Xcode6

When you do this, you can convert non-ARC projects to ARC projects.

How do I see if my project is ARC?

Search for auto in build settings, see options:

How do I make arc and non-arc coexist in a project?

Often need to use a third-party framework, or some other old code, then there is support for ARC, there is no support, how to do? This can be set: in the compilation options

To double-click a file that requires non-arc, set the following:

-fno-objc-arc

So this file will be able to use retain, release,autorelease and other keywords

-F represents the meaning of the flags mark, fixed notation.

In turn, for non-ARC projects, this setting:

-f-objc-arc

Arc Use note

    • Cannot call release, retain, Autorelease, Retaincount
    • can override Dealloc, but cannot invoke [super Dealloc]
    • @property: Want to have an object for a long time, should use strong, other objects with weak
    • Other basic data types still use assign
    • When both ends are referenced, one end is used with strong and one end with weak

Similarly, in the ARC project, there is also a circular double-ended reference phenomenon, you strong me, I strong your situation. The solution is as usual. When both ends are referenced, one end is used with strong and one end with weak

/*file name: Dog.h*/#import<Foundation/Foundation.h>@classPerson ;@interfaceDog:nsobject@property (nonatomic, weak) person*Person ;@end/*file name: dog.m*/#import "Dog.h"@implementationDog- (void) dealloc{NSLog (@"Dog--dealloc");}@end/*file name: Person.h*/#import<Foundation/Foundation.h>@classDog;@interfacePerson:nsobject@property (nonatomic, strong) Dog*Dog;@end/*file name: person.m*/#import "Person.h"@implementation Person- (void) dealloc{NSLog (@"Person--dealloc");}@end//main.m#import<Foundation/Foundation.h>#import "Person.h"#import "Dog.h"/*when the two ends of the circular reference, the solution: 1> ARC 1 End With strong, the other 1 end with weak 2> non-ARC 1 end with retain, the other 1 end with assign*/intMain () { person*p =[[Person alloc] init]; Dog*d =[[Dog alloc] init]; P.dog=D; D.person=p; return 0;}

Otherwise, it is also an error, such as using the Strong property

Person *p = [[Person alloc] init];         *d = [[Dog alloc] init];

Memory layout:

P.dog = d;//assigns the dog object to the _dog in the person object, and the pointer is a strong pointer.

D.person = p;//Similarly, the _person strong pointer in the dog object points to the person object

When the program executes, or the main function finishes, the automatic variable is destroyed

Because both are strong pointers, the memory leaks occur as in the case above. Therefore, __weak or weak properties are generally used in circular reference occasions, other occasions are rare.

Objective-c Grammar Quick over (7)

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.