[OC learning-26] Light copy and deep copy of objects-the key lies in whether attributes can be copied.

Source: Internet
Author: User

Objects can be copied in the shortest copy and deep copy modes. objects are copied only, but attributes are not copied. The copied objects share attributes with the original objects, that is, they point to the same attribute address, deep copy is equivalent to copying not only an object but also its attributes, that is, two things, but the content is the same.


The copy protocol is used. If the object is to be copied after this class is created, the copy protocol is used for this class. There are two types: <nscopying> and <nsmutablecopying> are equivalent to one copy, and the other can be modified after the copy.


(1) Case Study of shortest copy. There is a person class, which creates a person1 object and uses person1 to copy a person2 object.

// Person of the person class. h file # import <Foundation/Foundation. h> // because this class must support copying, the copy Protocol needs to be introduced. There are two types. After the latter type is copied, you can modify @ interface person: nsobject <nscopying, nsmutablecopying> @ property (nonatomic, copy) nsstring * Name; @ property (nonatomic, retain) nsnumber * Age; @ end

// This is person. M file # import "person. H "@ implementation person // This is a system function. You can copy it directly-(ID) copywithzone :( nszone *) Zone {person * person = [[self class] allocwithzone: zone] init]; // default format: person. name = _ name; // For a shortest copy, you can directly assign a value to person. age = _ age; // For a shortest copy, you can directly assign a value to return person;} @ end

// Main. M file # import <Foundation/Foundation. h> # import "person. H "// remember to introduce the header file int main (INT argc, const char * argv []) {@ autoreleasepool {person * person1 = [[person alloc] init]; [email protected] "Jack"; [email protected]; person * person2 = [person1 copy]; nslog (@ "% P, % P", person1, person2 ); // enter two object addresses, different nslog (@ "% P, % P", person1.age, person2.age); // output the attribute addresses of the two objects, same} return 0 ;}

Result:

0x1002036f0, 0x100200330 // different 0x1227,0x1227 // same

(2) Case study of deep copy.

To achieve deep copy, you only need to change the value assignment statement in person. m to the following:

person.name=[_name copy]; person.age=[_age copy];

However, since cocoa has been optimized, there are the following rules:

A: If it is an immutable object in the foundation framework, that is, an object created by array, nsstring, and so on, directly use copy to copy the object equivalent to retain, that is, the attribute is the same;

B: If mutablecopy is used to copy objects, whether they are mutable or immutable objects, the property Shenma directly copies one copy, that is, the copy is in the true sense, all the objects it copies are variable;

C: If it is a mutable object, we can use copy to copy objects in the true sense, but the copied objects are immutable.


Therefore, we use the name Experiment (because age does not have mutablecopy) to modify the statement:

person.name=[_name mutableCopy]person.age=[_age copy];

Then output the addresses of the name attributes of person1 and person2, and the results are different.


Summary:

The shortest copy and deep copy functions are not commonly used in actual projects.


[OC learning-26] Light copy and deep copy of objects-the key lies in whether attributes can be copied.

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.