CAT/CAT sorting problems: copy, object custom copy, custom copy

Source: Internet
Author: User

CAT/CAT sorting problems: copy, object custom copy, custom copy

CAT/CAT sharing, must be excellent
Original address: http://blog.csdn.net/u013357243? Viewmode = contents

Positive purpose of copycopy

Objective: To create a copy and modify each other without interfering with each other
Copy (immutable) and MutableCopy (mutable) data types for the Foundation framework.
For custom classes, copy is variable.

Copy of a variable array

Here we use NSMutableArray as an example.

// Variable = variable & immutable, the memory address will change void copyDemo1 () {NSMutableArray * arrayM = [NSMutableArray arrayWithObjects: @ (1), @ (2), nil]; NSLog (@ "% @ % p % @", arrayM. class); // 1. variable => variable NSMutableArray * aM = [arrayM mutableCopy]; NSLog (@ "% @ % p % @", aM, aM. class); // 2. variable => immutable NSArray * a = [arrayM copy]; NSLog (@ "% @ % p % @",. class );}

Let's draw a conclusion from the results.
1: arrayM, aM, and a are three different objects.
2: Variable => variable (arrayM is variable, and mutableCopy is used to get aM. class is NSArrayM)
3: Variable => immutable (aM is variable. Use copy to give a. class is NSArrayI)

Copy of an unchangeable Array
Void copyDemo2 () {NSArray * array = @ [@ (1), @ (2)]; NSLog (@ "% @ % p % @", array. class); // 1. immutable => variable NSMutableArray * aM = [array mutableCopy]; NSLog (@ "% @ % p % @", aM. class); // 2. immutable => immutable (Shortest copy) // pointer copy, reference count + 1 NSArray * a = [array copy]; NSLog (@ "% @ % p % @",. class );}

From the above results, we can draw a conclusion.
1: immutable => variable memory address changed, class from NSArrayI => NSArrayM
2: immutable => the immutable memory address is the same, and the class is composed of NSArrayI => NSArrayI
The function is to copy the pointer, and the reference count is + 1. Term (shallow copy)

3: Light replication: immutable => immutable deep replication: three other cases! (In this case, we can remember that we cannot change it to a light copy, or a deep copy, which is easier to remember than in memory 4 .)
Where is the most widely used?

Most of them are used here.

@property (nonatomic, copy) NSString *name;

NSString
Block

Both use the copy attribute and copy attribute. When assigning values, a copy operation is performed by default, making it an "unchangeable type" strong equivalent to a retain in MRC. When assigning values, only reference count + 1

What Yang's type is used in @ property to explain his methods.
For example, this Code:

NSMutableString * strM = [NSMutableString stringWithString: @ "zhangsan"]; NSLog (@ "% @ % p % @", strM. class); Person * p = [[Person alloc] init]; p. name = strM; NSLog (@ "% @ % p % @", p. name, p. name, p. name. class); // modify "Source", p. the name will be modified with [strM setString: @ "lisi"]; NSLog (@ "===%@ % p", strM, strM ); NSLog (@ "% @ % p", p. name, p. name );

If strong is used to describe the name of person, when we modify strM, p. name will also be changed. If we use copy, it will not be modified. It depends on @ property in the Person class.
If copy is used, a String is copied to the name.
If strong is used, the reference count is only + 1.
Can p. name be modified? Here we cannot directly modify it. We need to use the 10 thousand x pointer.

// Modify p. name id obj = p. name; [obj setString: @ "wangwu"]; NSLog (@ "===%@ % p", strM, strM ); NSLog (@ "% @ % p", p. name, p. name );

This can be changed with strong, so that the class will become insecure. Change strong to copy.
When we modify the "Source ",

[strM setString:@"lisi"];

P. name is still unchanged.
At this time, there is no problem in compiling the method for modifying attributes, but an error will be reported during running.

Attempt to mutate immutable object with xxx
View to modify an immutable type, use method xxx

For "variable type" attributes, do not use the copy Descriptor Definition. Otherwise, it cannot be changed after the value is assigned!

Copy custom object [object copy]

To enable the object to use the copy method [p copy]
(To implement the copy function for custom objects)
1> comply with the NSCopying Protocol (in essence, there are quick prompts when programmers write code)
2> Implementation-(id) copyWithZone :( NSZone *) zone
(Zone, region, rarely used)
All the copy methods will eventually call the copyWithZone method.
Copy an object and copy it to a new object.

-(Id) copyWithZone :( NSZone *) zone {// 1> instantiate the object. self is the object // self. class can ensure that the inherited subclass uses the copy method Person * p = [[self. class alloc] init]; // 2> assign p to the attribute. name = self. name; p. age = self. age; // 3> return the new object return p ;}

In this case, we call

void copyDemo5(){    Person *p = [[Person alloc] init];    p.name = @"zhangsan";    p.age = 18;    NSLog(@"%@", p);    Person *p1 = [p copy];    p1.name = @"lisi";    NSLog(@"%@", p1);}


In this case, we can call the copy method of the defined class object and generate an object.
Note: Here p1.name = @ "lisi" is not modified, but is re-assigned. Do not confuse copy.

Ps: Create an iOS communication learning group: 304570962
You can add a cat QQ: 1764541256 or znycat
Let's study hard together.
Http://blog.csdn.net/u013357243? Viewmode = contents

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.