Shallow copy: When copying an object, if the object contains an instance variable of the object type, just copy the pointer. The object type instance variable in the new object and the object type instance variable in the old object refer to the same object. Any one-party instance variable modifies the object, and the object that the other instance variable points to is changed.
. h Declaration File
1 //Person.h2 //03-Object Replication3 //4 //Created by Ma C on 15/8/14.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import<Foundation/Foundation.h>9 Ten @interfacePerson:nsobject<nscopying>//implementing the Nscopying protocol to support replication of custom objects
One @property (nonatomic,retain) nsmutablestring *name; // (retain) shallow copy, copy the object reference, its practical or an object, the equivalent of a person gave himself an alias
A @property (nonatomic,assign) Nsinteger age; -(ID) initwithname: (nsmutablestring*) name Andage: (Nsinteger) age; (void) show; the @end
. m implementation file
1 //person.m2 //03-Object Replication3 //4 //Created by Ma C on 15/8/14.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import "Person.h"9 Ten @implementation Person One-(ID) Initwithname: (nsmutablestring*) name Andage: (nsinteger) age A { -Self =[Super init]; - if(self) the { -_name =[name retain]; //strong reference to get object ownership -_age =Age ; - } + returnSelf ; - } +-(void) Show A { atNSLog (@"Name:%@,age:%lu", _name,_age); - } --(void) Dealloc - { - [_name release]; - [Super Dealloc]; in } --(ID) Copywithzone: (Nszone *) Zone to { + return[Person Alloc]initwithname:_name andage:_age]; - } the @end
main.m main function
1 //main.m2 //03-Object Replication3 //4 //Created by Ma C on 15/8/14.5 //Copyright (c) 2015 BJSXT. All rights reserved.6 //7 8 #import<Foundation/Foundation.h>9 #import "Person.h"Ten intMainintargcConst Char*argv[]) { One @autoreleasepool { A //Test Person Class -nsmutablestring *name = [nsmutablestring stringwithstring:@"Tom"]; - thePerson *P1 = [[Person alloc]initwithname:name Andage: -]; - [P1 show]; - -[Name appendString:@"Jerry"]; + -Person *P2 = [[Person alloc]initwithname:name Andage: +]; + [P2 show]; A at [P1 show]; - - //object replication, custom objects must implement the Nscopying protocol to support object replication -Person *P3 =[p1 copy]; - [P3 show]; - in[P1.name appendString:@"123"];//because Retain,p1 and P2 point to the same object, a change is - [P3 show]; to +nsmutablestring *name2 = [nsmutablestring stringwithstring:@"Jobs"]; -[P1 setname:name2];//P1 and P3 point to different objects that don't affect each other . the [P3 show]; * $ [P1 release];Panax Notoginseng [P2 release]; - [P3 release]; the } + return 0; A}
The test results are as follows:
-- ,- - -: +:59.293 Geneva-Object Copy [1460:96685] Name:tom,age: - -- ,- - -: +:59.294 Geneva-Object Copy [1460:96685] Name:tomjerry,age: + -- ,- - -: +:59.294 Geneva-Object Copy [1460:96685] Name:tomjerry,age: - -- ,- - -: +:59.295 Geneva-Object Copy [1460:96685] Name:tomjerry,age: - -- ,- - -: +:59.295 Geneva-Object Copy [1460:96685] Name:tomjerry123,age: - -- ,- - -: +:59.295 Geneva-Object Copy [1460:96685] Name:tomjerry123,age: -Program ended with exit code:0
Objective-c: Shallow copy (copy)