码迷,mamicode.com
首页 > 其他好文 > 详细

oc_对象的深拷贝

时间:2015-06-05 12:20:27      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:oc

#import "Dog.h"


@implementation Dog

- (id)copyWithZone:(NSZone *)zone

{

    Dog *dog = [[Dog allocWithZone:zone] init];

    

    dog.name = self.name;

    

    return dog;

}

@end

#import <Foundation/Foundation.h>


#import "Dog.h"


int main(int argc, const char * argv[])

{


    @autoreleasepool {

        

        //copy方法

        NSMutableArray *marr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",nil];

        NSArray *arr = [marr copy];

        

        //mutableCopy方法, 用于获取一个可变对象

        

        

        

        Dog *dog = [[Dog alloc] init];

        NSMutableString *mstr = [[NSMutableString alloc] initWithString:@"ahua"];

        dog.name = mstr;

        

        

        Dog *anthorDog = [dog copy];

        anthorDog.name = @"1212";

       // Dog *anthorDog = [Dog new];

        

        NSLog(@"dog name = %@  anthorDog name=%@",dog.name,anthorDog.name);

        

        

    }

    return 0;

}



oc_对象的深拷贝

标签:oc

原文地址:http://blog.csdn.net/wp1198571320/article/details/46374781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!