Ios copy statement

Source: Internet
Author: User

1. copy vs mutableCopy

Copy: For an immutable object, it simply points to its memory. For a mutable object, copy the memory content to the new memory and assign the new memory value to the left.

MutableCopy is always copied to the new memory and assigned to the left value as a variable type.

 

2. copy vc retain

Retain, reference count + 1, memory address assigned to the left value.

Copy: For immutable objects, it is equivalent to retain. For mutable objects, it is a deep copy assignment.

Example:

NSString * a = [NSString stringWithFormat: @ "% @", @ "this is a"]; NSString * B = [a copy]; NSString * bb = [a retain]; NSString * cc = [a mutableCopy]; // actually cc should be NSMutableString type NSLog (@ "% d, % d", [a retainCount], [B retainCount], [bb retainCount], [cc retainCount]); // output 3,3, 3,1 NSMutableString * a = [NSMutableString stringWithFormat: @ "% @", @ "this is a"]; NSString * B = [a copy]; // an unchangeable B NSString * bb = [a retain]; // bb NSString * cc = [a mutableCopy] For NSMutableString; // same as NSLog (@ "% d, % d ", [a retainCount], [B retainCount], [bb retainCount], [cc retainCount]); // outputs 2, 1, 2, and 1
3. Some Problems

Think about the following problems through the above two points

We usually define a variable like this.

@ Property (nonatomic, copy) NSMutableString * mString; then use

@ Synthesize mString; NSMutableString * a = [NSMutableString stringWithFormat: @ "% @", @ "this is a"]; self. mString = a; [mString insertString: @ "m-" atIndex: 0];
Can it pass? Of course not. The mString after the value assignment is of the NSString type and is immutable. You need to define the attribute function if you want to change the value.

-(Void) setMString :( NSMutableString *) m {mString = [m mutableCopy];}-(NSMutableString *) mString {return mString;} (of course, NSMutableString is NOT thread-safe, generally, it is recommended to use the private method: @ private; or use NSString as the external interface type if necessary)

4. NSCopying NSMutableCopying NSCopyObjective ()

NSCopying is to copy an object.

NSMutableCopying is a deep copy of an object, so that the changes of the two objects do not affect each other.

(In fact, the two above are totally dependent on how you write them)

NSCopyObject (self, 0, zone) is a simple value =

(NSCopyObject is not recommended when ns objects are involved)

Pay attention to the following example:

@ Interface ClassB: NSObject <NSCopying> {NSString * stringB;} @ property (nonatomic, copy) NSString * stringB; @ end-(id) copyWithZone :( NSZone *) zone {ClassB * B = NSCopyObject (self, 0, zone); // the correct value assignment method when using NSCopyObject, because it does not involve the original memory pointer, B-> stringB = @ "what"; // you can see the error method commented out. Due to the characteristics of the setter method, the original stringB points to the memory retainCount minus one // and because of the NSCopyObject feature, both point to the same address, so the address stirngB points to in the original class has been released, after that, will you release it once in dealloc ?!. An error occurred. // B. stringB = @ "what"; return B ;}

 

Reference: http://stackoverflow.com/questions/2002174/copy-mutablecopy

Http://stackoverflow.com/questions/4995254/nsmutablestring-as-retain-copy

Http://robnapier.net/blog/implementing-nscopying-439#comment-1312
Note: mutable object, such as NSMutableString and NSMutableArray
Immutable objects such as NSString and NSArray

Author: boundless

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.