IOS development-Stage 4-memory management, ios Stage 4

Source: Internet
Author: User

IOS development-Stage 4-memory management, ios Stage 4

Deep copy and light copy

 

Only objects that implement the NSCoping protocol can be copied. The NSCopy Protocol is required for copying custom objects. Whether it is a deep copy or a shallow copy depends on how the protocol is implemented.

 

The Type obtained by copy must be unchangeable; The type obtained by mutableCopy must be variable.

Shallow copy: After the copy, the memory address (pointer) remains unchanged;

Deep copy: the memory address (pointer) changes after the copy;

For an immutable copy of an immutable type, it is a shortest copy, and for others, it is a deep copy (whether it is a copy type or a copy method );

 

1) NSString: copy a shortest copy. After the copy, NSString is immutable;

MutableCopy: Deep copy. After the copy, the NSMutableString variable;

2) NSMutableString: copy deep copy. After the copy, NSString is immutable;

MutableCopy: Deep copy. After the copy, the NSMutableString variable;

(NSArray, NAMutableArray) (NSDictionary, NSMutableDictionary) NSSet (SET) is similar

3) A Strong is attached to copy.

4) @ autoreleasepool {} Automatically releases the pool. An object is released when an object is created in the for loop.

(5) NSAID utoreleasepool * pool = [NSAID utoreleasepool alloc] init];

 

Note:

Error code: @ property (nonatomic, copy) NSMutableArray * mutableArray;

When using this method, you must rewrite its set placement method;

Correct syntax: @ property (nonatomic, retain) NSMutableArray * mutableArray;

 

ARC

The [super delloc] Method in ARC cannot be called, but the delloc method is still valid. Therefore, you can still [[nsicationicationcenter defacenter center] removeObser: self];

 

Strong, Weak

1) if Strong is a Strong reference, the object pointed by strong pointer will never be released. If no Strong pointer points to it, it will be released immediately;

2) weak is a weak application. It is automatically set to null when its objects are released. objects without Strong or weak pointers are _ strong by default;

3) in ARC, the @ property retain object will be automatically compiled into Strong during compilation.

4) strong and weak do not modify simple types. assgin or unsafe_unretained is used for simple types.

5) in the ARC block, we used _ block to prevent circular references. Now we use weak;

 

Add a non-ARC file in the ARC environment:-fno-objc-arc

Add the ARC file-fobjc-ARC in a non-arc environment

 

Expansion:

Conversion, bridging

1). m OC and C can be directly mixed;

2). m --->. mm OC and C, C ++ mixing;

3). cpp C and C ++ are not mixed with OC;

4) _ bridge_retained OC ---> C is retaken once during conversion. (This is a global method and the method must be a method declared in. h );

5) _ bridge_transfer C ---> the OC will be release once during conversion;

6) _ bridge C <---> no changes are made to the reference count when OC is converted to each other. (The method must be a method declared in. h );

 

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.