First of all 、 The basic concept of copying objects

Copy an object as a copy , Open up a new memory to store replica objects .

second 、 If an object wants to have the ability to copy , Must be realized <NSCopying> The protocol and <NSMutableCopying> agreement

NSObject The commonly used objects are :NSNumber、NSString、NSArray、NSDictionary、NSMutableArray、NSMutableDictionay、NSMutableString,copy The resulting object is immutable ,mutableCopy The resulting object is variable

Third 、retain and copy The difference between

 @autoreleasepool {
NSMutableArray *array=[NSMutableArray arrayWithObjects:@"one",@"two",@"three",@"four",nil];
NSMutableArray *retainArray=[array retain];
[retainArray removeLastObject];
for(NSString *str in array)
{
NSLog(@"the part is %@",str);
}
NSLog(@"the retaincount is %ld",[retainArray retainCount]);
// insert code here...
NSLog(@"Hello, World!"); }

Fourth 、COPY and MutableCopy The difference between

COPY Returns a copy of an immutable object ,MutalbeCopy Returns a copy of a mutable object .

 NSArray *array=[NSArray arrayWithObjects:@"one",@"two", nil];
NSMutableArray *array1=[array copy];
[array1 addObject:@"three"]; //error
NSMutableArray *array2=[array mutableCopy];
[array2 addObject:@"three"]; //right
// insert code here...
NSLog(@"Hello, World!");

The fifth 、 shallow copy And deep copy

Shallow copy copies the object itself , Attributes in objects 、 The contained objects are not copied

Deep copy, copy all , Including the properties of the object and other objects

Foundation The framework supports replicated classes , The default is shallow copy

 NSMutableArray *array=[[NSMutableArray alloc] init];
for(int i=0;i<3;i++)
{
NSObject *obj=[[NSObject alloc] init];
[array addObject:obj];
[obj release];
}
for(NSObject *obj1 in array)
{
NSLog(@" The address is %p, The reference count is %ld",obj1,obj1.retainCount);
}
NSMutableArray *array2=[array copy];
for(NSObject *obj2 in array2)
{
NSLog(@" The address is %p, The reference count is %ld",obj2,obj2.retainCount);
}
2013-09-30 17:28:01.492 FDAS[681:303] The address is 0x1001081f0, The reference count is 1
2013-09-30 17:28:01.506 FDAS[681:303] The address is 0x100108230, The reference count is 1
2013-09-30 17:28:01.506 FDAS[681:303] The address is 0x100108240, The reference count is 1
2013-09-30 17:28:01.507 FDAS[681:303] The address is 0x1001081f0, The reference count is 2
2013-09-30 17:28:01.507 FDAS[681:303] The address is 0x100108230, The reference count is 2
2013-09-30 17:28:01.507 FDAS[681:303] The address is 0x100108240, The reference count is 2

In depth understanding of object C Copy object usage in ( One ) More articles about

  1. In depth understanding of object C Copy object usage in ( Two )

    The fifth . Custom copies of objects Objects have replication properties , Must be realized NSCopying,NSMutableCopying agreement , Implementation of the protocol copyWithZone Methods and mutableCopyWithZone Method Deep copy ...

  2. ASP.NET in application Object usage ( Interview questions )

    ASP.NET in application Object usage Introduction to this article :Application The object is HttpApplicationState An instance of a class ,Application State is global to the entire application .Appli ...

  3. java Copy objects through reflection or serialization

    After using the cache to read data, the cache is modified . So I found a way to copy objects . About object cloning As I understand it , An object is a reference that contains + data . In general, variable replication is to pass the reference . such as : Person p1 = new Person ...

  4. EMF Copy object properties in

    1. The simple scenario is to copy one EObject, You can use methods in tool classes EcoreUtil.copy(). 2. scene : Their own TO Class inherited EMF Class created , You need to copy all the properties in the parent class . /** * All the properties of the parent class ...

  5. ASP.NET in application Object usage

    One .Application Understanding of the object Application The purpose of object in the actual network development is to record the information of the whole network , For example, the number of people online . Online list . Opinion surveys and online elections, etc . Sharing information among multiple users of a given application , And in service ...

  6. CorelDRAW How to copy object attributes in

    Copying object properties is a special way . Important replication methods , It can easily and quickly transfer the outline of the specified object to the pen . Outline color . Fill and text properties are applied to the selected object by copying . This tutorial will explain CorelDRAW How to copy object properties in . Corel ...

  7. 【 turn 】JavaScript Object replication in (Object Clone)

    JavaScript Object replication is not directly provided in (Object Clone) Methods . So change the object in the following code b When , It changes the object a. a = {k1:1, k2:2, k3:3}; b = a; b. ...

  8. JavaScript Deep copy of objects in (Object Deep Clone)

    JavaScript Object replication is not directly provided in (Object Clone) Methods . JavaScript Assignment in , It's not actually copying objects , It's like `c/c++` Citation in ( Or pointer ), So change the object in the following code b ...

  9. Yes Object Deep understanding of methods in classes

    to glance at API About China Object Introduction to : class Object Is the root class of the class hierarchy . Each class uses Object As a superclass . All objects ( Including arrays ) All methods that implement this class . that Object What are the ways in the world , What do they have ...

Random recommendation

  1. CentOS7(mini) Deploy rapidly ASP.NET application

    from mono 2.8+ I started to pay attention to linux function .NET Some news from I have nothing to do recently , Find out mono The official source has been compiled , Greatly facilitates the deployment process , You don't have to compile it yourself mono 了 use Nancy.Owin Wrote a ...

  2. ashx Learning from

    original text :ashx Learning from Hey , Today we have a rest , I wanted to summarize what I learned in the first two weeks javascript and jquery, But I feel sleepy , I didn't study hard , So I did a little exercise , Just beginning to learn html Used in a project or ...

  3. git fetch, git pull analyse

    Really understand git fetch, git pull Make it clear git fetch,git pull, It must be made clear git remote,git merge . long-range repo, branch . commi ...

  4. Testlink1.9.17 Usage method ( Chapter two Sign in &amp; Chinese settings )

    Chapter two Sign in & Chinese settings QQ Communication group :585499566 1, Use a super account admin/admin Sign in . 2, After logging in , You will be prompted to create a new project , Don't create , First in user management , Set to Chinese display , That is, sinicization ...

  5. Odd-e CSD Course Day 4

    Part of today is through Code Review To trigger our thoughts on the following directions Good Unit Tests In today's Code Review Li ,Stanly It turns out that we wrote Unit Test ...

  6. Paging query information ( Use jdbc Connect mysql Database to achieve paging query task )

              Paging query information        Use jdbc Connect mysql Database to achieve paging query task adopt mysql The paging mechanism provided by the database , Realize the paging query function of commodity information , Display the information to jsp On the page . This project ...

  7. dpkg Use records

    dpkg -l  View all installed packages grep You can filter what you want dpkg -r  Package name    //  Uninstall package     -P  Uninstall completely   There may be configuration files that cannot be deleted   If you can't delete it, restart it and then uninstall it dpkg -i  package ...

  8. Several query methods (lambda Linq Enumerable Static class mode )

    1. You need a data source class : using System; using System.Collections.Generic; namespace Linq { public class Student { ...

  9. python logging Module usage

    http://blog.csdn.net/zyz511919766/article/details/25136485/ import logging logging.debug('debug mess ...

  10. Half plane intersection (poj 1279( The first half plane NlogN) Complete comments )

    Half planes intersect O(nlogn) Algorithm ( Reprint ) seek n There are three ways of intersection of two half planes : The first is to use each plane to cut the existing convex polygon , Complexity O(n^2). The second is the legendary divide and conquer algorithm . take n The first half plane is divided into two parts , Ask for the end of the intersection respectively ...