MutableCopy & copy

分別遵照<NSCopying>和 <NSMutableCopying>協議,http://blog.csdn.net/tskyfree/article/details/7999620 c++

首先了解深複製,淺複製: 數組

淺複製和深複製是對於包含對象成員的對象而言的。
淺複製:只複製對象自己,對象的成員只複製指針。
深複製:在淺複製的基礎上,同時複製對象的成員。 app

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://(淺複製) ui

#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的第一個元素都被改變了.

//下面的例子:(深複製) atom

//這種方式只會改變一個數組中的對象,而對另一個沒有影響  
            mStr = [NSMutableString stringWithString:[dataArray2 objectAtIndex:0]];  
            [mStr appendString:@"ONE"];  
            [dataArray2 replaceObjectAtIndex:0 withObject:mStr];


下面是StackoverFlow對MutableCopy和Copy的區別的解釋: spa

// ** 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:
相關文章
相關標籤/搜索