#import <Foundation/Foundation.h>

@interface Father : NSObject <NSCopying,NSMutableCopying>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,retain) NSNumber *age;
-(id) initWithName:(NSString *)name withAge:(NSNumber *) age;
@end
#import "Father.h"
#import "Child.h" @implementation Father -(id) initWithName:(NSString *)name withAge:(NSNumber *) age{
self=[super init];
if(self!=nil){
_name=name;
_age=age;
}
return self;
}
//浅拷贝
-(id) copyWithZone:(NSZone *)zone{
Father *father=[[[self class] allocWithZone:zone] init];
father.name=_name;
father.age=_age;
return father;
} //深拷贝
- (id)mutableCopyWithZone:(NSZone *)zone{
Father *father=[[[self class] allocWithZone:zone] init];
father.name=[_name mutableCopy];
father.age=[_age copy];
return self;
}
 NSLog(@"-------自定义对象浅拷贝---------");
//浅拷贝学习
Father *father=[[Father alloc] initWithName:@"caitou" withAge:[NSNumber numberWithInt:23]];
Father *father2=[father copy];
NSLog(@"对象地址:%p",father);
NSLog(@"对象地址:%p",father2);
NSLog(@"对象属性地址name:%p",[father name]);
NSLog(@"对象属性地址name:%p",[father2 name]);
NSLog(@"对象属性地址age:%p",[father age]);
NSLog(@"对象属性地址age:%p",[father2 age]); NSLog(@"--------自定义对象深拷贝---------");
//深拷贝
Father *father4=[[Father alloc] initWithName:@"caitou" withAge:[NSNumber numberWithInt:25]];
Father *father3=[father4 mutableCopy];
NSLog(@"对象地址:%p",father4);
NSLog(@"对象地址:%p",father3);
NSLog(@"对象属性地址name:%p",[father4 name]);
NSLog(@"对象属性地址name:%p",[father3 name]);
NSLog(@"对象属性地址age:%p",[father4 age]);
NSLog(@"对象属性地址age:%p",[father3 age]); NSLog(@"-------浅拷贝---------");
NSArray *array1=[[NSArray alloc] initWithObjects:@"sdf", nil];
NSArray *array2=[array1 copy];
NSLog(@"%p",array1);
NSLog(@"%p",array2); NSLog(@"-------深拷贝---------");
NSMutableArray *mutableArray=[NSMutableArray arrayWithObject:@"sdfdf" ];
NSMutableArray *mutableArray2=[mutableArray mutableCopy];
NSLog(@"%p",mutableArray);
NSLog(@"%p",mutableArray2);
//总结:当copy一个不可变对象时,相当有retain,即引用记数加一,其他情况copy,mutablecopy都会分配空间复制内容

浅拷贝,深拷贝---ios的更多相关文章

  1. JS中有关对象的继承以及实例化、浅拷贝深拷贝的奥秘

    一.属性的归属问题 JS对象中定义的属性和方法如果不是挂在原型链上的方法和属性(直接通过如类似x的方式进行定义)都只是在该对象上,对原型链上的没有影响.对于所有实例共用的方法可直接定义在原型链上这样实 ...

  2. $.extend()浅拷贝深拷贝

    参考网址:http://bijian1013.iteye.com/blog/2255037 jQuery.extend() 函数用于将一个或多个对象的内容合并到目标对象. 注意:1. 如果只为$.ex ...

  3. Python__学习路上的坑之--引用,浅拷贝,深拷贝

    copy : 相当于只是拷贝表面一层,如果里面还有深层次的引用,那么也是直接拷贝引用的地址,而且如果拷贝对象是不可变类型比如元组,那么也是直接拷贝引用. deepcopy: 无论是拷贝可变类型还是不可 ...

  4. Python中的浅拷贝 深拷贝

    浅拷贝只拷贝父对象,子对象的地址空间不改变,包括下面三种: 1. copy 从下面的例子可以看出对象c从a拷贝,当对象a增加一个列表元素之后,c对象没有改变, 而当对象a中的子列表改变时,对象c的子列 ...

  5. 浅拷贝 &&&深拷贝 实现

    1.浅拷贝 //1.直接赋值给一个变量 //浅拷贝 //2.Object.assign() //浅拷贝 let obj4={} let obj5={money:50000} obj4.__proto_ ...

  6. javascript浅拷贝深拷贝详解

    一.浅拷贝 浅拷贝在现实中最常见的表现在赋值上面,例如 <!DOCTYPE html> <html lang="en"> <head> < ...

  7. js库 - 浅拷贝 & 深拷贝

    学了堆栈内存空间,应该就理解了什么叫简单数据类型存在栈内存,复杂数据类型存在堆内存了. 然后面试中经常会问.业务中也经常会遇到的问题就是深浅拷贝的问题了. 栈内存中简单数据类型直接拷贝就能得到一个副本 ...

  8. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  9. python中赋值-浅拷贝-深拷贝之间的关系

    赋值: 变量的引用,没有拷贝空间 对象之间赋值本质上 是对象之间的引用传递而已.也就是多个对象指向同一个数据空间. 拷贝的对象分两种类型: . 拷贝可变类型 浅拷贝: 只拷贝第一层数据,不关心里面的第 ...

  10. javascript浅拷贝深拷贝理解记录

    javascript的深拷贝和浅拷贝问题几乎是面试必问的问题.好记性不如烂笔头,特此来记录一下自己对深拷贝浅拷贝的理解. 顾名思义,拷贝就是copy复制,在js中可以浅而理解为对一个对象或者数组的复制 ...

随机推荐

  1. Connection to http://www.google.com:80 refused

    使用SDK Manager更新时出现问题 Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.x ...

  2. 2016 - 1 - 3 国旗选择demo

    // // ViewController.m // 国旗 // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rights re ...

  3. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  4. EF中逆变和协变

    EF中的增删改查: 实现步骤: 1.声明一个EF的上下文. bjhksjEntities dbContext = new bjhksjEntities(); 2.声明一个实体. HKSJ_USERS ...

  5. 10、C#基础整理(集合)

    集合 1.集合的引用 using System.Collections;//添加类 2.定义集合(ArrayList 或 Array) ArrayList arr = new ArrayList(); ...

  6. 解决input之间的空隙

    <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...

  7. NBU 2475 Survivors(RMQ线段树)

    NBU 2475Survivors 题目链接:http://acm.nbu.edu.cn/v1.0/Problems/Problem.php?pid=2475 题意:给定n个人,每个人有strengt ...

  8. 分布式系统之CAP理论杂记[转]

    分布式系统之CAP理论杂记 http://www.cnblogs.com/highriver/archive/2011/09/15/2176833.html 分布式系统的CAP理论: 理论首先把分布式 ...

  9. Windows Server 2008 R2: 创建任务计划

    task Scheduler 在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步数据库,定时发送电子邮件等. 创建一个任务计划: 开始St ...

  10. ‘Cordova/CDVViewController.h’ file not found Xcode 7.1

    Add this line to your Build Settings -> Header Search Paths: "$(OBJROOT)/UninstalledProducts ...