ios学习路线—Objective-C(深浅拷贝)

在ObjC中,什么是深浅拷贝?  深浅拷贝分别指深拷贝和浅拷贝,即 mutableCopy 和 copy 方法。  copy复制一个不可变对象,而 mutableCopy 复制一个 mutable 可变对象。

 

非容器类对象  如NSString,NSNumber等一类对象 
示例1:

// 非容器类对象
    NSString *str = @"origin string";
    NSString *strCopy = [str copy];
    NSMutableString *mstrCopy = [str mutableCopy];
    [mstrCopy appendString:@"??"];

//    NSLog(@"array1     = %p", array1);
//    NSLog(@"arrayCopy1 = %p", arrayCopy1);

查看内存可以发现,str和strCopy指向的是同一块内存区域,我们称之为弱引用(weak reference)。而mstrCopy是真正的复制,系统为其分配了新内存空间,保存从str复制过来的字符串值。从最后一行代码中修改这些值而不影 响str和strCopy中可证明。

示例2:

    NSMutableString *mstr = [NSMutableString stringWithString:@"origin"];
    NSString *strCopy = [mstr copy];
    NSMutableString *mstrCopy = [mstr copy];
    NSMutableString *mstrMCopy = [mstr mutableCopy];
    //[mstrCopy appendString:@"1111"];  //error
    [mstr appendString:@"222"];
    [mstrMCopy appendString:@"333"];

以上四个对象所分配的内存都是不一样的。而且对于mstrCopy,它所指向的其实是一个imutable对象,是不可改变的,所以会出错。这点要注意,好好理解。

小结: 
1.如果对一个不可变对象复制,copy是指针复制,即浅拷贝;而mutableCopy则是对象复制,即深拷贝。 
2.如果是对可变对象复制,都是深拷贝,但是copy复制返回的对象是不可变的。

容器类对象深浅复制 
比如NSArray,NSDictionary等。对于容器类本身,上面讨论的结论也适用的,下面探讨的是复制后容器内对象的变化。

示例3:

    /* copy返回不可变对象,mutablecopy返回可变对象 */
    NSArray *array1     = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
    NSArray *arrayCopy1 = [array1 copy];
    //arrayCopy1是和array同一个NSArray对象(指向相同的对象),包括array里面的元素也是指向相同的指针
    NSLog(@"array1 retain count: %d",[array1 retainCount]);      // 2
    NSLog(@"array1 retain count: %d",[arrayCopy1 retainCount]);    //  2

    NSMutableArray *mArrayCopy1 = [array1 mutableCopy];
    //mArrayCopy1是array1的可变副本,指向的对象和array1不同,但是其中的元素和array1中的元素指向的还是同一个对象。mArrayCopy1还可以修改自己的对象
    [mArrayCopy1 addObject:@"de"];
    [mArrayCopy1 removeObjectAtIndex:0];

array1和arrayCopy1是指针复制,而mArrayCopy1是对象复制,符合前面示例1讨论的结论。mArrayCopy1可以改变其内的元素:删除或添加。但容器内的元素内容都是浅拷贝。

 

示例4

    NSArray *mArray1 = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"a"],@"b",@"c",nil];
    NSLog(@"mArray1 retain count: %d",[mArray1 retainCount]);
    NSArray *mArrayCopy2 = [mArray1 copy];
    NSLog(@"mArray1 retain count: %d",[mArray1 retainCount]);
    // mArray1和mArrayCopy2指向同一对象,retain值+1。

    NSMutableArray *mArrayMCopy1 = [mArray1 mutableCopy];
    NSLog(@"mArray1 retain count: %d",[mArray1 retainCount]);
    //mArrayCopy2和mArray1指向的是不一样的对象,但是其中的元素都是一样的对象——同一个指针

    NSMutableString *testString = [mArray1 objectAtIndex:0];
    //testString = @"1a1";//这样会改变testString的指针,其实是将@“1a1”临时对象赋给了testString
    [testString appendString:@" tail"];//这样以上三个数组的首元素都被改变了

由此可见,对于容器而言,其元素对象始终是指针复制。如果需要元素对象也是对象复制,就需要实现深拷贝。

 

示例5

NSArray *array = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"first"] ,[NSString stringWithString:@"b"],@"c",nil];
NSArray *deepCopyArray=[[NSArray alloc] initWithArray: array copyItems: YES];
NSLog(@"array[2] = %@, deepCopyArray[2]=%@ ",[array objectAtIndex:2], [deepCopyArray objectAtIndex:2]);  //输出值是一样的
NSLog(@"array[2]         %p", [array objectAtIndex:2]);
NSLog(@"deepCopyArray[2] %p", [deepCopyArray objectAtIndex:2]);   
  //最后两个打印的log内存地址值是一样的


NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: array]];

trueDeepCopyArray 是完全意义上的深拷贝,而deepCopyArray则不是,对于 deepCopyArray 内的不可变元素其还是指针复制。

 

自己实现深拷贝的方法 NSDictionaryMutableDeepCopy.h

#import <foundation /Foundation.h>
@interface NSDictionary(MutableDeepCopy)
- (NSMutableDictionary *)mutableDeepCopy;
@end

NSDictionaryMutableDeepCopy.m

#import "NSDictionaryMutableDeepCopy.h"
@implementation NSDictionary(MutableDeepCopy)
- (NSMutableDictionary *)mutableDeepCopy {
    NSMutableDictionary *ret = [[NSMutableDictionary alloc]
                                initWithCapacity:[self count]];
    NSArray *keys = [self allKeys];
    for (id key in keys) {
        id oneValue = [self valueForKey:key];
        id oneCopy = nil;
        if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]) {
            oneCopy = [oneValue mutableDeepCopy];
        }
        else if ([oneValue respondsToSelector:@selector(mutableCopy)]) {
            oneCopy = [oneValue mutableCopy];
        }
        if (oneCopy == nil) {
            oneCopy = [oneValue copy];
        }
        [ret setValue:oneCopy forKey:key];
    }
    return ret;
}

使用类别方法来实现 
如果是我们定义的对象,那么我们自己要实现NSCopying,NSMutableCopying这样就能调用copy和mutablecopy了。举个例子

@interface MyObj : NSObject<nscopying ,NSMutableCopying>
{
         NSMutableString *name;
         NSString *imutableStr;
         int age;
}
@property (nonatomic, retain) NSMutableString *name;
@property (nonatomic, retain) NSString *imutableStr;
@property (nonatomic) int age;

@end

@implementation MyObj
@synthesize name;
@synthesize age;
@synthesize imutableStr;
- (id)init
{
         if (self = [super init])
         {
                   self.name = [[NSMutableString alloc]init];
                   self.imutableStr = [[NSString alloc]init];
                   age = -1;
         }
         return self;
}

- (void)dealloc
{
         [name release];
         [imutableStr release];
         [super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
         MyObj *copy = [[[self class] allocWithZone:zone] init];
         copy->name = [name copy];
         copy->imutableStr = [imutableStr copy];
//       copy->name = [name copyWithZone:zone];;
//       copy->imutableStr = [name copyWithZone:zone];//
         copy->age = age;
         return copy;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
    MyObj *copy = NSCopyObject(self, 0, zone);
    copy->name = [self.name mutableCopy];
    copy->age = age;
    return copy;
}
@end

ios学习路线—Objective-C(深浅拷贝)

上一篇:android添加阴影


下一篇:Android探究之Gson@SerializedName