iOS开发那些事儿(五)Objective-C浅拷贝与深拷贝

  1. iOS开发那些事儿(五)Objective-C浅拷贝与深拷贝
  2. 浅拷贝:copy操作出来的对象指针直接指向模板的地址。即两个对象公用一块内存地址
     #import <Foundation/Foundation.h>
    int main(int argc, const char * argv[]) {
    @autoreleasepool {
    NSString * testStr = @"connor";
    NSLog(@"testStr's Address = %p",testStr); NSString * testStrCopy = [testStr copy];
    NSLog(@"testStrCopy Address = %p",testStrCopy); NSArray * array = @[@,@,@];
    NSLog(@"array Address = %p",array); NSArray * copyArray = [array copy];
    NSLog(@"copyArray Address = %p",copyArray);
    }
    return ;
    }

    输出结果如下:

    DataStruct[11210:2189074] testStr's Address      = 0x100004280

    DataStruct[11210:2189074] testStrCopy Address = 0x100004280

    DataStruct[11210:2189074] array Address            = 0x100300650

    DataStruct[11210:2189074] copyArray Address     = 0x100300650

  3. 深拷贝:copy操作出来的对象指针直接指向新开辟的内存。即持有原对象的拷贝副本
     #import <Foundation/Foundation.h>
    #import "Father.h"
    int main(int argc, const char * argv[]) {
    @autoreleasepool {
    NSString * test = @"a";
    NSLog(@"%p",test); NSString * testCopy = [test mutableCopy];
    NSLog(@"%p",testCopy);
    }
    return ;
    }

    输出结果如下:

    DataStruct[11355:2249168] 0x100004280

    DataStruct[11355:2249168] 0x100203cf0

  4. Foundation总结大致如下:
    •   NS*类型的类调用Copy属于浅拷贝(例如NSString、NSArray等等)
    •   NS*类型的类调用MutableCopy属于深拷贝(例如NSString、NSArray等等)
    •   NSMutable*类型无论调用Copy或者MutableCopy都属于深拷贝
  5. 拷贝构造:以上我们谈的都是Foundation中用到的深浅拷贝,如果我们自己定义了一个类。怎么去实现它的深浅拷贝呢?这里就要用到拷贝构造方法。iOS中默认NSObject是不遵循NSCopying(不变副本)、NSMutableCopying(可变副本)的,所以如果想一个对象可Copy,就必须实现其中两个协议并且重写copyWithZone、mutableCopyWithZone方法。
    •   下面我们定义一个Father类,来实现深拷贝:
       /** .h  */
      #import <Foundation/Foundation.h>
      @interface Father : NSObject<NSCopying>
      @property(nonatomic,copy)NSString * name;
      @end /** .m */
      #import "Father.h" @implementation Father
      -(id)copyWithZone:(NSZone *)zone{
      Father * copyFather = [[self class]allocWithZone:zone];
      return copyFather;
      }
      @end

      写好了Father类我们在外界就可以利用copy的方法去copy出一个Father对象

       #import <Foundation/Foundation.h>
      #import "Father.h"
      int main(int argc, const char * argv[]) {
      @autoreleasepool {
      Father * fatherA = [[Father alloc]init];
      fatherA.name = @"connor";
      NSLog(@"%p",fatherA); Father * fatherB = [fatherA copy];
      NSLog(@"%@",fatherB.name);
      NSLog(@"%p",fatherB);
      }
      return ;
      }

      输出结果:

      DataStruct[11383:2256524] 0x100206c80

      DataStruct[11383:2256524] (null)

      DataStruct[11383:2256524] 0x100300600

      从输出结果来看,两个Father实例的内存地址是不一样的我们实现了深拷贝。但是为什么第二个Father没有名字?在拷贝过程过虽然我是从你FatherA拷贝过来的,但是你并没有指定FatherA中的属性到底是什么方式拷贝过来。所以这里面如果想要实现名字也拷贝过来,需要我们自己去定义到底是深拷贝过来还是浅拷贝过来。这也符合苹果做事的风格嘛。

       #import "Father.h"
      //添加name的浅拷贝
      @implementation Father
      -(id)copyWithZone:(NSZone *)zone{
      Father * copyFather = [[self class]allocWithZone:zone];
      copyFather.name = [self.name copy]; //如果想要深拷贝过来直接mutableCopy即可
      return copyFather;
      }
      @end

      结果输出两个name的内存地址:

      DataStruct[11409:2261412] 0x100004290

      DataStruct[11409:2261412] 0x100004290

    • 现在情况改变了,Father平时出门需要开车。Father需要持有Car类。那这个时候我们要如果复制这个爸爸!(因为复制出来的Father不能和之前那个Father开一辆车,所以我们应该用深拷贝。过程很简单直接贴代码了!)
       /** .h */
      #import <Foundation/Foundation.h> @interface Car : NSObject<NSCopying>
      @property(nonatomic,copy)NSString * brand;
      @end /** .m */
      @implementation Car
      -(id)copyWithZone:(NSZone *)zone{
      Car * copyCar = [[self class]allocWithZone:zone];
      copyCar.brand = [self.brand mutableCopy];
      return copyCar;
      }
      @end

      Car

      /** .h */
      #import <Foundation/Foundation.h>
      #import "Car.h"
      @interface Father : NSObject<NSCopying>
      @property(nonatomic,copy)NSString * name;
      @property(nonatomic,strong)Car * car;
      @end /** .m */
      #import "Father.h" @implementation Father
      -(id)copyWithZone:(NSZone *)zone{
      Father * copyFather = [[self class]allocWithZone:zone];
      copyFather.name = [self.name copy];
      copyFather.car = [self.car copy];
      return copyFather;
      }
      @end

      Father

      #import <Foundation/Foundation.h>
      #import "Father.h"
      #import "Car.h"
      int main(int argc, const char * argv[]) {
      @autoreleasepool {
      Father * fatherA = [[Father alloc]init];
      fatherA.car = [[Car alloc]init];
      NSLog(@"%p",fatherA.car); Father * fatherB = [fatherA copy];
      NSLog(@"%p",fatherB.car);
      }
      return ;
      }

      main

      输出结果:

      DataStruct[11441:2268004] 0x100111f60

      DataStruct[11441:2268004] 0x1001154a0

    • copy这种操作如果发生在继承关系之后就有什么样的效果?或者说1.父类实现了NSCopying,子类该如果实现NSCopying。2.父类没有实现NSCopying,子类如何实现NSCopying?要接一个问题我们先看下表象是什么,然后通过表象来分析问题并解决问题。接下来我们先试着用第一种情况来看看表象:Son继承于Father
       /** .h */
      #import "Father.h"
      @interface Son : Father
      @property(nonatomic,copy)NSString * toy;
      @end /** .m */
      #import "Son.h"
      @implementation Son
      @end

      Son

       #import <Foundation/Foundation.h>
      #import "Son.h"
      int main(int argc, const char * argv[]) {
      @autoreleasepool {
      Son * sonA = [[Son alloc]init];
      sonA.name = @"connor";
      sonA.toy = @"iPhone"; Son * sonB = [sonA copy]; NSLog(@"sonA's Address = %p",sonA);
      NSLog(@"sonB's Address = %p",sonB); NSLog(@"sonA's name Address = %p",sonA.name);
      NSLog(@"sonB's name Address = %p",sonB.name); NSLog(@"sonA's toy Address = %p",sonA.toy);
      NSLog(@"sonB's toy Address = %p",sonB.toy);
      }
      return ;
      }

      main

      输出结果:

      DataStruct[11563:2279201] sonA's Address      = 0x100114bc0

      DataStruct[11563:2279201] sonB's Address          = 0x100111f70

      DataStruct[11563:2279201] sonA's name Address = 0x100004260

      DataStruct[11563:2279201] sonB's name Address = 0x726f6e6e6f6365

      DataStruct[11563:2279201] sonA's toy Address    = 0x100004280

      DataStruct[11563:2279201] sonB's toy Address = 0x0                                                                                  这个结果也在我们意料之内的嘛,Son是继承与Father类的,Father实现了Copy协议,所以这个copy协议实现会继承到子类。我的理解其实:当Son调用Copy方法的时候就将消息转发给Son抽象类,发现Son中没有实现Copt则向Father发送消息SuperSendMsg就相当于事件响应链传递一样。一直向上传递直到能被处理。**这里需要注意一点Father中  Father * copyFather = [[self class]allocWithZone:zone];这里用的是self而不是Father因为Son会走到这里来,我们不能限制死到底是谁来调用***。接下来我们继续看为什么toy是个空置,这个原因和上面说的嵌套copy有点类似。因为Father不清楚你Son的属性到底是怎么copy过来的。那么怎么修改呢?我们只要在toy中稍作修改即可

       @implementation Son
      -(id)copyWithZone:(NSZone *)zone{
      //这句话保证父类的copy也被call到
      Son * son = [super copyWithZone:zone];
      son.toy = [self.toy mutableCopy];
      return son;
      }
      @end

      Son

      输出结果:toy成功被深拷贝

      DataStruct[11606:2301994] sonA's toy Address = 0x100004288

      DataStruct[11606:2301994] sonB's toy Address = 0x656e6f68506965

    • 接下来我们看一下第二种情况,父类没有实现NSCopying,子类如何实现NSCopying。直接让子类去实现就好了呗。父类如果需要深拷贝的我延迟到子类去执行呗!(这里有个问题,我的Son没有继承NSCopying,但是重写CopyWithZone居然没有报错??!!)
       #import "Son.h"
      @implementation Son
      -(id)copyWithZone:(NSZone *)zone{
      // Son * son = [super copyWithZone:zone];
      Son * son = [[self class] allocWithZone:zone];
      son.name = [self.name mutableCopy];
      son.toy = [self.toy mutableCopy];
      return son;
      }
      @end

      Son

      输出结果:

      DataStruct[11660:2317623] sonA's Address          = 0x100206e00

      DataStruct[11660:2317623] sonB's Address          = 0x1002040e0

      DataStruct[11660:2317623] sonA's name Address = 0x100004260

      DataStruct[11660:2317623] sonB's name Address = 0x726f6e6e6f6365

      DataStruct[11660:2317623] sonA's toy Address    = 0x100004280

      DataStruct[11660:2317623] sonB's toy Address    = 0x656e6f68506965

上一篇:java程序:set改造成map


下一篇:基础知识系列☞Abstract和Virtual→及相关知识