Copy解密

来源:互联网 发布:英克软件 下载 编辑:程序博客网 时间:2024/06/05 22:47

浅拷贝只复制对象本身,而不会复制对象引用的其它对象,深拷贝除了复制对象本身,还会复制对象所引用的其它对象,copy返回不可变对象(包括可变对象在内),mutableCopy返回可变对象

#import <Foundation/Foundation.h>
#import "Animal.h"
#import "Cat.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Animal *animal1 = [[Animal alloc] init];//实例的创建初始化
        animal1.dog = [[Dog alloc]init];
        animal1.weight = 10;//属性的赋值
        animal1.dog.age = 3;
        NSLog(@"%d,%d",animal1.weight,animal1.weight);

       
        Animal *animal2 = [animal1 copy];//对象的深度复制
        NSLog(@"%@,%d",animal1.dog,animal1.dog.age);//地址发生改变
        NSLog(@"%@,%d",animal2.dog,animal2.dog.age);
        [animal1 print];
        [animal2 print];
       

        //需要实现NSCopying或者NSMutableCopying协议才能使用复制功能,Foundation中的基础数据类型如NSString、NSNumber等都 实现了NSCopying
        NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"好好学习",@"天天向上", nil];// 数组的创建并初始化
        NSMutableArray *arr1 = [arr copy];//放回不可变数组
        NSMutableArray *arr2 = [arr mutableCopy];//放回可变数组
        NSLog(@"%@%@%@",[arr class],[arr1 class],[arr2 class]);
        
        [arr addObject:@"我考上研了!"];//数组添加元素
        NSLog(@"%@",arr);
        
        Dog *d1 = [[Dog alloc] init];//对象的创建、初始化、复制
        Dog *d2 = [d1 copy];
        Cat *c1 = [[Cat alloc] init];
        Cat *c2 = [c1 copy];
        
//        NSArray *arry = [NSArray arrayWithObjects:c1,d2,c2,d1, nil];;//定义数组并初始化
//        for (int i = 0; i < [arry count]; i++)
//        {
//            if ([[arry objectAtIndex:i] isKindOfClass:[Dog class]])//数组中的某个元素是否属于某个类
//            {
//                id animal = [arry objectAtIndex:i];
//                [animal bite];
//            }
//            else
//            {
//                id animal = [arry objectAtIndex:i];
//                [animal scratch];
//            }
//        }
        NSArray *arry = [NSArray arrayWithObjects:c1,c2,d1,d2,nil];//定义数组并初始化
        //快速枚举
        for(id animal in arry)
        {
            if ([animal isKindOfClass:[Dog class]])//判断是否为某个类的实例
            {
                [animal performSelector:@selector(bite)];//执行bite方法
            }
            else if ([animal isKindOfClass:[Cat class]])
            {
                [animal performSelector:@selector(scratch)];
            }
        }
    }
    return 0;
}


接口文件1:Animal.h

#import <Foundation/Foundation.h>
#import "Dog.h"

@interface Animal : NSObject <NSCopying>//需要实现NSCopying或者NSMutableCopying协议才能使用复制功能
@property (nonatomic,assign) int weight;
@property (nonatomic,retain) Dog *dog;
- (void) print;

@end



实现文件1:Animal.m

#import "Animal.h"

@implementation Animal
- (id) copyWithZone:(NSZone *)zone
{
    Animal *animal = [[[self class] allocWithZone:zone] init];//创建并初始化
    animal.weight = _weight;//实例变量的复制
    animal.dog = [_dog copy];//对于是对象的实例变量必须把对象所指向的内容复制一次,不能简单的只复制对象所存的地址
    return animal;
}
- (void) print
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
}
@end


接口文件2:Dog.h

#import <Foundation/Foundation.h>

@interface Dog : NSObject <NSCopying>
@property (nonatomic,assign) int age;
- (void) bite;
@end


实现文件2:Dog.m#import "Dog.h"

@implementation Dog
- (id) copyWithZone:(NSZone *)zone
{
    Dog *dog = [[[self class] allocWithZone:zone]init];
    dog.age = _age;
    return dog;
}
- (void) bite
{
    NSLog(@"bite");
}
@end


接口文件3:Cat.h

#import "Animal.h"

@interface Cat : Animal <NSCopying>
- (void) scratch;
@end



实现文件3:Cat.m#import "Cat.h"


@implementation Cat
- (id) copyWithZone:(NSZone *)zome
{
    Cat *cat = [Cat new];
    return cat;
}
- (void) scratch
{
    NSLog(@"Sratch");
}

@end

0 0