不务正业之OC(十一)–复制对象之深浅Copy

浅Copy只复制对象本身,不对对象属性进行复制。foundatio框架中支持对象Copy的,默认是浅Copy。

#import <Foundation/Foundation.h>
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (int i = 1; i < 3; i++) {
            NSObject *obj = [[NSObject alloc] init];
            [array addObject:obj];
            [obj release];
        }
 
        for (NSObject *o in array) {
            NSLog(@"address:%@,retain count:%lu",o,(unsigned long)o.retainCount);
        }
 
        NSMutableArray *arraycopy = [array copy];
        for (NSObject *b in arraycopy) {
            NSLog(@"address:%@,retain count:%lu",b,(unsigned long)b.retainCount);
        }
    return 0;
    }
}
2014-05-03 12:00:44.643 Objcopy[15694:303] address:<NSObject: 0x1002022c0>,retain count:1
2014-05-03 12:00:44.645 Objcopy[15694:303] address:<NSObject: 0x100201b30>,retain count:1
2014-05-03 12:00:44.645 Objcopy[15694:303] address:<NSObject: 0x1002022c0>,retain count:2
2014-05-03 12:00:44.646 Objcopy[15694:303] address:<NSObject: 0x100201b30>,retain count:2

可以发现原对象的地址没发生变化,只是引用计数加1

浅Copy:

//  My.h
//  Copytest
 
#import <Foundation/Foundation.h>
 
@interface My : NSObject<NSCopying>
 
@property(nonatomic,retain) NSString* name;
@property(nonatomic,assign) NSNumber* age;
 
@end
 
//  My.m
//  Copytest
 
#import "My.h"
 
@implementation My
 
//浅Copy
- (id)copyWithZone:(NSZone *)zone{
    My *my = [[[self class] allocWithZone:zone] init];
    my.name = _name;
    my.age = _age;
    return my;
}
@end
 
//  main.m
//  Copytest
#import <Foundation/Foundation.h>
#import "My.h"
 
int main(int argc, const char * argv[])
{
 
    @autoreleasepool {
        My *my = [[My alloc] init];
        my.name = @"lijie";
        my.age = @31;
 
        My *my2 = [my copy];
        NSLog(@"%my is address:%p",my);
        NSLog(@"%my2 is address:%p",my2);
 
        NSLog(@"my name address:%p",my.name);
        NSLog(@"my2 name address:%p",my2.name);    
    }
    return 0;
}
//浅Copy的属性变量name指向同一个地址
2014-05-03 12:57:28.264 Copytest[16051:303] my is address:0x100202340
2014-05-03 12:57:28.266 Copytest[16051:303] my2 is address:0x1002033f0
2014-05-03 12:57:28.266 Copytest[16051:303] my name address:0x1f27
2014-05-03 12:57:28.267 Copytest[16051:303] my2 name address:0x1f27

深Copy
只修改copyWithZone方法

#import "My.h"
 
@implementation My
//浅Copy
- (id)copyWithZone:(NSZone *)zone{
    My *my = [[[self class] allocWithZone:zone] init];
    my.name = [_name mutableCopy];//Foundation可复制对象,当我们Copy一个不可变对象时,作用就是retain,cocoa做内存优化。使用mutableCopy,无论对象是否可变,副本是可以变,就实现真正的Copy。Copy可变的对象时,当副本是不可变,也实现Copy
    my.age = [_age copy];
    return my;
}
@end
 
2014-05-03 15:01:59.308 Copytest[16189:303] my is address:0x100203f00
2014-05-03 15:01:59.309 Copytest[16189:303] my2 is address:0x100202ae0
2014-05-03 15:01:59.310 Copytest[16189:303] my name address:0x1000022f0 //地址不一样
2014-05-03 15:01:59.310 Copytest[16189:303] my2 name address:0x100204560

Post a Comment