MutableCopy & copy

分别遵守<NSCopying>和 <NSMutableCopying>协议,http://blog.csdn.net/tskyfree/article/details/7999620

首先了解深复制,浅复制:

浅复制和深复制是对于包含对象成员的对象而言的。
浅复制:只复制对象本身,对象的成员只复制指针。
深复制:在浅复制的基础上,同时复制对象的成员。

object c中深拷贝 与传统c++深拷贝一样,都是为了生成一个对象的新的副本,然后把成员按照原来的对象附值。

object c 中需要继承协议NSCopying 并实现方法-(id)copyWithZone:(NSZone *)zone;

例子:
@interface DeepCopy : NSObject<NSCopying>
{
    NSString* mName;  
}

.m文件中
- (id)copyWithZone:(NSZone *)zone
{
    DeepCopy* aCopy = [[[self class] allocWithZone:zone]init];
    todo.mName = [self mName];//也可以todo.mName = [[self mName] copy];
    return aCopy;   
}//copyWithZone:(NSZone *)zone

 主函数中测试下:
      DeepCopy *aDeep1 = [[DeepCopy alloc] init];
      aDeep1.mName = @"liurui";

      DeepCopy *aDeep2 = [aDeep1 copy];

经过测试发现:
     1. aDeep1 和 aDeep2的 内存地址是不同的,说明成功拷贝了一个副本;
     2. 继续跟踪内存发现,aDeep1 aDeep2 对象中的属性mName地址是相同的;




例子1(直接在回复框里写的,不保证能直接运行):

@interface A : NSObject {
}

@property (nonatomic, retain) NSString * member;
- (A *) shadowCopy;
- (A *) deepCopy;

@end

@implementation A
@synthesize member;
- (void) dealloc {
    [self.member release];
    [super dealloc];
}

- (A *) shadowCopy {
    A * ret = [A alloc];
    ret.member = self.member;
    return [ret autorelease];
}

- (A *) deepCopy {
    A * ret = [A alloc];
    ret.member = [NSString stringWithString:self.member]; // 这就是本质区别
    return [ret autorelease];
}

@end

例子2://(浅复制)

#import <Foundation/Foundation.h>  
  
int main(int argc, const char * argv[])  
{  
  
    @autoreleasepool {  
          
        NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:  
                                     [NSMutableString stringWithString:@"one"],  
                                     [NSMutableString stringWithString:@"two"],  
                                     [NSMutableString stringWithString:@"three"],  
                                     nil  
                                     ];  
        NSMutableArray *dataArray2;  
        NSMutableString *mStr;  
          
        NSLog(@"dataArray:   ");  
        for(NSString *elem in dataArray)  
            NSLog(@"   %@", elem);  
          
        //执行一个拷贝,然后改变其中的一个字符串(浅复制)  
        dataArray2 = [dataArray mutableCopy];  
          
        //这种方式会同时改变连个数组中的对象  
        mStr = [dataArray objectAtIndex:0];  
        [mStr appendString:@"ONE"];  
                  
        NSLog(@"dataArray:");  
        for(NSString *elem in dataArray)  
            NSLog(@"  %@",elem);  
          
        NSLog(@"dataArray2:");  
        for(NSString *elem in dataArray2)  
            NSLog(@"  %@",elem);  
          
        [dataArray2 release];  
    }  
    return 0;  
} 
//上面的例子dataArrary和dataArray2的第一个元素都被改变了.

//下面的例子:(深复制)

    //这种方式只会改变一个数组中的对象,而对另外一个没有影响  
            mStr = [NSMutableString stringWithString:[dataArray2 objectAtIndex:0]];  
            [mStr appendString:@"ONE"];  
            [dataArray2 replaceObjectAtIndex:0 withObject:mStr];  


下面是StackoverFlow对MutableCopy和Copy的区别的解释:

// ** NSArray **
NSArray *myArray_imu = [NSArray  arrayWithObjects:@"abc", @"def", nil];

// No copy, increments retain count, result is immutable
NSArray *myArray_imuCopy = [myArray_imu copy];

// Copys object, result is mutable 
NSArray *myArray_imuMuta = [myArray_imu mutableCopy];

[myArray_imuCopy release];
[myArray_imuMuta release];

// ** NSMutableArray **
NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil];

// Copys object, result is immutable
NSMutableArray *myArray_mutCopy = [myArray_mut copy];

// Copys object, result is mutable
NSMutableArray *myArray_mutMuta = [myArray_mut mutableCopy];

[myArray_mutCopy release];
[myArray_mutMuta release];



 
 

   // mutableCopy always returns a mutable result.
   // copy always returns an immutable result.
 


//
copy and mutableCopy are defined in different
 protocols (NSCopying and NSMutableCopying, respectively), 
and NSArray conforms to both. mutableCopy is defined
 for NSArray (not just NSMutableArray) and allows you to make a mutable 
copy of an originally immutable array:




你可能感兴趣的:(MutableCopy & copy)