iOS 学习(深拷贝 浅拷贝)

copy与mutableCopy的区别,如果学过c或者c++的朋友会知道指针这样一个概念,就是在有指针的情况下,浅拷贝只是增加了一个指针指向已经存在的内存

而深拷贝就是不仅增加了一个指针,并且还申请了一个新的内存,是这个新增加的指针指向这个新的内存,采用深拷贝的情况下,释放内存的时候就不会出现在浅拷贝时重复释放同一内存的错误。

//
//  main.m
//  learn02
//
//  Created by lee on 15/12/26.
//  Copyright © 2015年 ltybrp. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *marray = [NSMutableArray arrayWithObjects:
                                  [NSMutableString stringWithString:@"one"],
                                  [NSMutableString stringWithString:@"tow"],
                                  [NSMutableString stringWithString:@"three"],
                                  nil];
        NSMutableArray *marray_copy = [[NSMutableArray alloc] init];

        marray_copy = [marray mutableCopy];
        NSMutableString *mstr = [marray_copy objectAtIndex:0];
        [mstr appendString:@"ONE"];

        NSLog(@"%@", [marray_copy description]);
        printf("------------------------------");
        NSLog(@"%@",[marray description]);


        NSMutableString * str = [NSMutableString stringWithString:@"hello"];
        NSMutableString * str_copy = [str mutableCopy];

        [str_copy appendString:@"World "];

        NSLog(@"%@", str);
        printf("---------------=--------");
        NSLog(@"%@", str_copy);

    }
    return 0;
}

我们可以看一下运行结果,
2015-12-28 15:12:05.588 learn02[969:28440] (
oneONE,
tow,
three
)
——————————2015-12-28 15:12:05.589 learn02[969:28440] (
oneONE,
tow,
three
)
2015-12-28 15:12:05.589 learn02[969:28440] hello
—————=——–2015-12-28 15:12:05.589 learn02[969:28440] helloWorld
Program ended with exit code: 0

其实 NSMutableArray中的 MutableCopy 拷贝实际上考了的事新的引用,虽然 MutableCopy拷贝后得到了一个新的副本,但是副本中拷贝的内容是引用, 新旧两个MutableArray中都是两个引用,并且指向同一个地址,所以无论通过哪个更高,都会使得原来字符串变动,

要实现自定义的类进行copy必须根据协议实现其中一两个方法。

实现协议时,必须实现copyWithZone:方法来响应copy消息, 如果想要区分 可变副本和不可变副本, 还需要根据协议实现mutableCopyWithZone:方法,如果两个方法都实现,则 copyWithZone:返回不可变副本mutableCopyWithZone:返回可变副本,产生对象的可变副本并不要求被复制的对象本身也是可变的, 反之亦然,想要产生不可变对象的可变副本时合理的。

Fraction.h

#import <Foundation/Foundation.h>
@interface Fraction : NSObject <NSCopying>
@property int numerator;
@property int  denominator;
-(id) copyWithZone:(NSZone *)zone;
-(void) setTo: (int ) A over: (int ) B;
-(void) print;

@end
//
//  Fraction.m
//  learn02
//
//  Created by lee on 15/12/28.
//  Copyright © 2015年 ltybrp. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

@synthesize denominator;
@synthesize numerator;
-(void) setTo:(int ) A over:(int )B{
    self.denominator = B;
    self.numerator = A;
}

-(void) print{
    printf("%d \\ %d\n", self.numerator, self.denominator);
}

-(id) copyWithZone:(NSZone *)zone{
    Fraction *newFract = [[Fraction allocWithZone:zone] init];
    [newFract setTo:numerator over: denominator];
    return newFract;
}

@end
//
//  main.m
//  learn02
//
//  Created by lee on 15/12/26.
//  Copyright © 2015年 ltybrp. All rights reserved.
//

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Fraction *f1 = [[Fraction alloc]init];
        Fraction *f2 = [[Fraction alloc] init];

        [f1 setTo:2 over:5];
        f2 = [f1 copy];
        [f2 setTo:3 over:7];
        [f1 print];
        [f2 print];

    }
    return 0;
}

结果
2 \ 5
3 \ 7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值