struct student
{
int a; float f; char c; long l;
}; struct person
{
int a; float f; char c; long l; };
void test1(void)
{
int a=; int b; memcpy(&b, &a , sizeof(a)); NSLog(@"b=%i",b);
struct student stu1={,1.5,'x',}; struct student stu2; memcpy(&stu2, &stu1, sizeof(stu1)); NSLog(@"%i, %f,%c, %ld",stu2.a,stu2.f,stu2.c,stu2.l); // struct person per=stu1; struct person per; memcpy(&per, &stu1, sizeof(stu1)); NSLog(@"per: %i,%f,%c,%ld",per.a,per.f,per.c,per.l);
}
void testStringCopy(void)
{
NSString* s1=[[NSString alloc]initWithFormat:@"name:%s","wenhua" ];
NSString* s2 = [s1 copy]; //浅拷贝 NSLog(@"s1:%p, %@",s1,s1); NSLog(@"s2:%p, %@",s2,s2); NSMutableString* s3 = [s1 copy]; NSLog(@"s3:%p,%@",s3,s3); // 不可变字符串的copy方法: 1,浅拷贝,2,还是不可变 NSString* s4 = [s1 mutableCopy]; //深拷贝 NSLog(@"s4:%p,%@",s4,s4); [(__bridge id)(__bridge void *)s4 appendString:@"append"]; NSLog(@"s4:%p,%@",s4,s4); NSLog(@"s1:%p, %@",s1,s1); // 不可变字符串的mutableCopy方法: 1,深拷贝,2,拷贝出来的是可变的 NSMutableString* s5=[[NSMutableString alloc]initWithString:@"string"]; NSMutableString* s6 = [s5 copy]; //深拷贝,但s6不可变 NSLog(@"s5: %p,%@",s5,s5); NSLog(@"s6: %p,%@",s6,s6); // [s6 appendString:@"appendStringFors6"]; // 可变字符串的copy方法: 1,深拷贝,2,,拷贝出来的是不可变的 NSMutableString* s7 = [s5 mutableCopy];//深拷贝,s7是可变的 NSLog(@"s7: %p,%@",s7,s7); [s7 appendString:@"appendStringFors7"]; NSLog(@"s7: %p,%@",s7,s7); // 可变字符串的mutableCopy方法: 1,深拷贝, 2,拷贝出来的是可变的 /* 总结: 拷贝 可变拷贝 copy mutableCopy 不可变字符串NSString调: 浅,不可变 深,可变 可变NSMutableString调: 深,不可变 深,可变     你想啊,mutableCopy后,为了两个对象修改后互不影响,一定是深拷贝啦,不会只是拷贝指针;可变字符串copy后,自然是深拷贝,因为一个可变一个不可变;不可变字符串copy,只需要浅拷贝就行喽! */
} void testArrayCopy(void)
{
/*
不可变数组和可变数组的拷贝情况: 不可变字典和可变字典的拷贝情况:
*/ NSString* s1=@"abcd"; // NSArray* a1=@[@"1",@"2"]; NSMutableArray* a1 = [[ NSMutableArray alloc]initWithArray:@[@"",@""]]; Person* person = [[ Person alloc]init]; NSLog(@"person计数:%lu",[person retainCount]); [a1 addObject:person]; NSLog(@"person计数:%lu",[person retainCount]); NSArray* array1 = [[NSArray alloc]initWithObjects:s1,a1, nil]; NSArray* array2 =[array1 copy]; NSLog(@"array1:%p,%@",array1,array1); NSLog(@"array2:%p,%@",array2,array2); NSMutableArray *array3 = [array1 copy]; NSLog(@"array3:%p,%@",array3,array3); NSMutableArray * array4 = [array1 mutableCopy]; //可变 NSLog(@"array4:%p,%@",array4,array4); NSLog(@"array1:[0]:%p,[1]:%p",array1[],array1[]); NSLog(@"array4:[0]:%p,[1]:%p",array4[],array4[]); [array4 addObject:@"add"]; NSLog(@"array4 count is %lu",[array4 count]);//array4是可变数组 [array1 release]; [array2 release]; [array3 release]; [array4 release]; [person release]; [a1 release];
} void test3(void)
{
Person *person =[[Person alloc]init]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *array = [[ NSMutableArray alloc]init]; [array addObject:person]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *newArray = [array mutableCopy];//拷贝了一层 NSLog(@"person的计数:%lu",[person retainCount]); [array release]; [newArray release]; [person release];
} void test4(void)
{
Person* person = [[ Person alloc]init]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *array = [[ NSMutableArray alloc]init]; [array addObject:person]; NSLog(@"person的计数:%lu",[person retainCount]); //对可变数组调copy方法, NSMutableArray *newArray = [array copy]; // ? 拷贝出来的数组可不可变? 拷了几层? // [newArray addObject:@"add"]; //回答:拷贝出来的数组不可变 NSLog(@"person的计数:%lu",[person retainCount]);
[array release];
[newArray release];
[person release];
}
void test2(void)
{
Person* person = [[ Person alloc]init]; //问:为什么NSString类的实例变量在property语法中要用copy? NSMutableString* name=[[NSMutableString alloc]initWithString:@"wenhua"]; person.name=name; NSLog(@"person.name:%p,name:%p",person.name,name); [name appendFormat:@".奥斯特洛夫斯基"]; NSLog(@"person.name: %@",person.name); //因为传入person对象的name是可变字符串对象,如果用retain来修饰,则会造成在外边就可以修改person对象的实例变量,不符合封装的思想,所以用copy } //拷贝自定义的对象 void copyForObject(void) { /* 理论: 1, 对象如果要调copy方法,就要遵守NSCopy协议 2, 对象如果要调mutableCopy方法,就要遵守NSMutableCopying协议 但是注意: copy方法 不是协议NSCopying的方法 mutableCopy方法 也不是协议NSMutableCopying的方法 copy和mutableCopy都是NSObject实现的方法 因为: 在NSObject实现的copy方法内部调用了协议NSCopying的方法 在NSObject实现的mutableCopy方法内部调用了协议NSMutableCopying的方法 NSCopying规定的方法名: copyWithZone: NSMutableCopying规定的方法名:       mutableCopyWithZone:
*/ /*
#import "Person.h" @implementation Person //协议NSCopying规定的一个方法,这个方法由copy方法内部来调 -(id)copyWithZone:(NSZone *)zone {
Person* person = [[[self class] allocWithZone:zone ]init]; person.name=self.name; return person;
} -(id)mutableCopyWithZone:(NSZone *)zone {
Person* person = [[[self class] allocWithZone:zone ]init]; person.name=self.name; return person;
} @end */
Person* p1 = [[ Person alloc]init]; Person *p2 = [p1 copy];//p1可以调copy,是因为Person类实现了copyWithZone:方法 NSLog(@"p1:%p,p2:%p",p1,p2); Person* p3 = [ p1 mutableCopy];//p1可以调mutableCopy,是因为Person类实现了mutableCopyWithZone:方法 NSLog(@"p3:%p",p3);
}

父类中实现子类的深拷贝

- (id)copyWithZone:(NSZone *)zone {
id copyInstance = [[[self class] allocWithZone:zone] init];
size_t instanceSize = class_getInstanceSize([self class]);
memcpy((__bridge voidvoid *)(copyInstance), (__bridge const voidvoid *)(self), instanceSize);
return copyInstance;
}

//***********************************************

关于容器实现copy或mutableCopy,容器内元素默认都是指针拷贝

除非使用

NSArray *trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:array]];

实现元素的内容拷贝,深拷贝.

copy和mutableCopy对容器本身是深拷贝,其内部元素是指针拷贝.

//类对象调用copy后,为深拷贝,但是类中属性为指针拷贝

        ApplePie *pie = [[ApplePie alloc]init];
pie.name = @"I am an apple pie"; ApplePie *ala = [pie copy]; NSLog(@"%p %p",ala.name,pie.name);// 相同的地址
NSLog(@"%p %p",ala,pie);// 不同的地址

引用:http://www.cnblogs.com/tangbinblog/p/3964003.html

NSCopy&NSMutableCopy的更多相关文章

  1. iOS开发系列—Objective-C之Foundation框架

    概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NSObject,那么这个对象到底 ...

  2. iOS-几大框架的介绍

    1.Objective-C之Foundation框架 概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objectiv ...

  3. iOS-Objective-C基础

    一.Foundation框架 概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NS ...

  4. Objective -C学习笔记 之copy(复制)

    //自定义类对象实现copy需要遵守copy协议(否则程序崩溃),实现必须实现的协议方法,里面的代码就决定了你的copy是深是浅 #import <Foundation/Foundation.h ...

  5. 理解Objective-c中的copy

    说一下深拷贝和浅拷贝的基本概念:a指针指向地址A1, 浅拷贝是创建了一个b指针指向地址A1:深拷贝是创建了一个c指针指向地址A2,A1和A2的地址不同. 我们看到NSObject接口里面是已经声明了c ...

  6. OC学习12——字符串、日期、日历

    前面主要学习了OC的基础知识,接下来将主要学习Foundation框架的一些常用类的常用方法.Foubdation框架是Cocoa编程.IOS编程的基础框架,包括代表字符串的NSString(代表字符 ...

  7. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  8. iOS开发200个tips总结(一)

    tip 1 :  给UIImage添加毛玻璃效果 func blurImage(value:NSNumber) -> UIImage { let context = CIContext(opti ...

  9. NSFileManager 的基本使用方法

    本方法已有个人总结, int main(int argc, const char * argv[]) { @autoreleasepool { NSString *path=@"/Users ...

随机推荐

  1. eclipse build workspace太慢或者 js出错问题解决

    1.js文件错误解决办法 右键项目->properties->Builders(注:JavaScript Validator也会引起 build workspace太慢) 2.Eclips ...

  2. Python深入06 Python的内存管理

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 语言的内存管理是语言设计的一个重要方面.它是决定语言性能的重要因素.无论是C语言的 ...

  3. The network bridge on device VMnet0 is not running

    The network bridge on device VMnet0 is not running. The virtual machine will not be able to communic ...

  4. 数据可视化-EChart2.0使用总结2

    接上一篇博客,这篇博客主要讨论EChart里面的散点图.气泡图和雷达图.   4.散点图-Scatter Chart 适合场景:三维数据集,但是只有两个维度需要比较.比较的是X轴和Y轴的数据,第三个数 ...

  5. linux标准IO缓冲(apue)

    为什么需要标准IO缓冲? LINUX用缓冲的地方遍地可见,不管是硬件.内核还是应用程序,内核里有页高速缓冲,内存高速缓冲,硬件更不用说的L1,L2 cache,应用程序更是多的数不清,基本写的好的软件 ...

  6. 小菜学习Winform(一)贪吃蛇2

    前言 上一篇<小菜学习Winform(一)贪吃蛇>中实现了简单版的贪吃蛇,在文章末也提到需要优化的地方,比如使用oo.得分模式.速度加快模式和减少界面重绘.因为是优化篇,实现方式上一篇有, ...

  7. selenium操作隐藏的元素

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: Python 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操 ...

  8. (七)WebGIS中栅格、矢量图层设计之栅格、矢量图层的本质

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.何为栅格数据,何为矢量数据? 在GIS中,对于数据格式的分类,我们 ...

  9. 通过 WebSocket 实现 WebGL 3D 拓扑图实时数据通讯同步(二)

    我们上一篇<基于 WebSocket 实现 WebGL 3D 拓扑图实时数据通讯同步(一)>主要讲解了如何搭建一个实时数据通讯服务器,客户端与服务端是如何通讯的,相信通过上一篇的讲解,再配 ...

  10. 分离与继承的思想实现图片上传后的预览功能:ImageUploadView

    本文要介绍的是网页中常见的图片上传后直接在页面生成小图预览的实现思路,考虑到该功能有一定的适用性,于是把相关的逻辑封装成了一个ImageUploadView组件,实际使用效果可查看下一段的git效果图 ...