一个对象使用copy或mutableCopy方法可以创建对象的副本

1.copy
需要实现NSCopying协议
创建出来的是不可变副本,如NSString, NSArray, NSDictionary
 
(1)不可变对象调用copy  (NSSring除外)
不会产生新的对象,而是返回对象本身,相当于retain,计数器+1
属于浅拷贝

         NSArray *arr1 = [NSArray arrayWithObjects:@"张三", @"李四", nil];
NSLog(@"arr1.addr: %p", arr1); // 0x1002052b0
NSLog(@"arr1.retainCount: %ld", arr1.retainCount); // NSArray *arr2 = [arr1 copy];
NSLog(@"arr1.addr: %p", arr1); // 0x1002052b0
NSLog(@"arr1.retainCount: %ld", arr1.retainCount); // 2 <-- 浅拷贝,相当于retain, retainCount+1
NSLog(@"arr2.addr: %p", arr2); //0x1002052b0 <-- 浅拷贝,依旧指向同一个对象
NSLog(@"arr2.retainCount: %ld", arr2.retainCount); // 2 <-- 浅拷贝,还是原来对象的retainCount
 
(2)可变对象调用copy (NSMutableString会拷贝出一个NSString”常量对象")
返回一个不可变对象,但是不是原来的对象,属于深拷贝
         NSMutableArray *arr1 = [NSMutableArray array];
[arr1 addObject:@""];
[arr1 addObject:@"abc"];
NSLog(@"arr1.addr: %p", arr1); // 0x100204840
NSLog(@"arr1.retainCount: %ld", arr1.retainCount); // NSMutableArray *arr2 = [arr1 copy];
NSLog(@"arr1.addr: %p", arr1); // 0x100204840
NSLog(@"arr1.retainCount: %ld", arr1.retainCount); // 1 <-- 深拷贝,原来的对象retainCount保持
NSLog(@"arr2.addr: %p", arr2); // 0x100300000 <-- 深拷贝,开辟了新的内存空间,拷贝出来的实际对象是NSArray
NSLog(@"arr2.retainCount: %ld", arr2.retainCount); // 1 <-- 深拷贝,新的对象retainCount独立
 
(3)重点: NSString的特殊性
由于使用了 @“” 进行初始化,数据存放在了常量池
 
因为NSString指向字符串常量,系统不会收回,也不会对其作引用计数,即使我们对NSString变量如何retain或release,retainCount都是-1 (无符号最大值)
         NSString *str1 = [[NSString alloc] initWithString:@"abc"];
NSLog(@"str1.addr: %p", str1); // 0x100001030
NSLog(@"str1.retainCount: %ld", str1.retainCount); // -1 <-- 字符串常量存在常量池,不使用堆内存 NSString *str2 = [str1 copy];
NSLog(@"str1.addr: %p", str1); // 0x100001030
NSLog(@"str1.retainCount: %ld", str1.retainCount); // -1 <-- 无论是copy, retain, release操作都不会改变retianCount
NSLog(@"str2.addr: %p", str2); // 0x100001030 <-- 拷贝之后的变量依旧指向原来的常量
NSLog(@"str2.retainCount: %ld", str2.retainCount); // -1 <-- 原来常量的retainCount [str1 retain];
NSLog(@"after retain -> str1.retainCount: %ld", str1.retainCount); // -1 <-- 无论是copy, retain, release操作都不会改变retianCount
[str2 release];
NSLog(@"after release -> str2.retainCount: %ld", str2.retainCount); // -1 <-- 无论是copy, retain, release操作都不会改变retianCount
 
要使一个NSString变量也有retainCount:
就要是指向另外一个”NSString”对象,而不是字符串常量
         // 先创建出一个NSString对象,再用另一个指向
NSString *str1 = [NSString stringWithFormat:@"abc"];
NSLog(@"%ld", str1.retainCount); // -1
NSString *str2 = [NSString stringWithString:str1];
NSLog(@"%ld", str2.retainCount); //
 
(4)copy出来的是不可变对象,如NSMutableString调用copy创建出来的实际是NSString
         // 这里不能使用 NSMutableString *mstr1 = @"abc", 或者在延迟赋值 mstr1 = @"abc"; 否则同样指向常量池
NSMutableString *mstr1 = [[NSMutableString alloc] initWithString:@"abc"];
NSLog(@"mstr1.addr: %p", mstr1); // 0x100404990
NSLog(@"mstr1.retainCount: %ld", mstr1.retainCount); // NSMutableString *mstr2 = [mstr1 copy];
NSLog(@"mstr1.addr: %p", mstr1); // 0x100404990
NSLog(@"mstr1.retainCount: %ld", mstr1.retainCount); // 1 <-- 深拷贝,原来的对象retainCount保持
NSLog(@"mstr2.addr: %p", mstr2); // 0x63626135 <-- 深拷贝,开辟了新的内存空间,但是指向的实际是一个字符串常量
NSLog(@"mstr2.retainCount: %ld", mstr2.retainCount); // -1 <-- 因为其实拷贝出来的是一个字符串常量
2.mutableCopy
需要实现NSMutableCopying协议
创建的是可变副本,如NSMutableString, NSMutableArray, NSMutableDictionary
 
(1)不可变对象调用mutableCopy
产生一个新的对象,新旧对象的计数器独立,不会互相干扰,属于深拷贝
         NSString *str1 = [[NSString alloc] initWithString:@"abc"];
NSLog(@"str1.addr: %p", str1); // 0x100001030
NSLog(@"str1.retainCount: %ld", str1.retainCount); // -1 <-- 字符串常量存在常量池,不使用堆内存 NSMutableString *mstr2 = [str1 mutableCopy]; // mutableCopy 出来的是一个 NSMutableString 对象
NSLog(@"str1.addr: %p", str1); // 0x100001030
NSLog(@"str1.retainCount: %ld", str1.retainCount); // -1 <-- 对于字符串常量, 无论是copy, retain, release操作都不会改变retianCount
NSLog(@"mstr2.addr: %p", mstr2); // 0x100106460 <-- 深拷贝,开辟新的空间
NSLog(@"mstr2.retainCount: %ld", mstr2.retainCount); // 1 <-- 由于是NSMutableString对象,retainCount == 1
 
(2)可变对象调用mutableCopy
产生一个新的对象,新旧对象的计数器独立,不会互相干扰,属于深拷贝
         NSMutableString *mstr1 = [[NSMutableString alloc] initWithString:@"abc"];
NSLog(@"mstr1.addr: %p", mstr1); // 0x100402910
NSLog(@"mstr1.retainCount: %ld", mstr1.retainCount); // NSMutableString *mstr2 = [mstr1 mutableCopy]; // mutableCopy 出来的是一个 NSMutableString 对象
NSLog(@"mstr1.addr: %p", mstr1); // 0x100402910
NSLog(@"mstr1.retainCount: %ld", mstr1.retainCount); //
NSLog(@"mstr2.addr: %p", mstr2); // 0x100300e90 <-- 深拷贝,开辟新的空间
NSLog(@"mstr2.retainCount: %ld", mstr2.retainCount); //
 
3.自定义类的copy
 @interface Student : NSObject

 //copy代表setter会release旧对象,copy新对象
@property (nonatomic, copy) NSString *name; @end
 
传入外部NSMutableString对象到student的setter,当外部对象改变的时候,不会改变student中copy来的对象
如果使用retain或者assign,就会影响,因为指向的是同一个对象
==>传入NSMutableString时(成员变量为NSString),一般使用copy策略,其他使用retain
 
(1)实现<NSCopying>
(2)实现 - (id) copyWithZone:(NSZone *) zone
 - (id)copyWithZone: (NSZone *) zone
{
Student *stu = [[Student allocWithZone:zone] init];
stu.name = self.name;
return stu;
}
 
 

[OC Foundation框架 - 17] copy语法的更多相关文章

  1. OC Foundation框架—集合

    Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...

  2. OC Foundation框架—结构体

    一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...

  3. OC Foundation框架—字符串

    一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...

  4. iOS - OC Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...

  5. [OC Foundation框架 - 20] 统计代码行数

    注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量   // // main.m // CodeLineCount // // Created ...

  6. OC — (Foundation框架-NSDate)

    NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...

  7. [OC Foundation框架 - 23] 文件管理

    A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...

  8. [OC Foundation框架 - 15] NSDate

    日期处理类 定义 时间间隔计算 时间比较,返回其一 时间格式化 void dateCreate() { //date return current time NSDate *date = [NSDat ...

  9. [OC Foundation框架 - 13] NSValue

    NSNumber能够包装基本数据类型称为OC对象,是因为继承了NSValue 包装结构体成OC对象 1.自带结构体 void value() { CGPoint point = CGPointMake ...

随机推荐

  1. highcharts 切换

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  2. 李洪强iOS开发之XMPP

      XMPP历史 这个xmpp框架在2008年开始,不过是一个简单地RFC实现.提供一个最小的代理去接受三种xmpp的基本类型presence.message.iq.因为framwork只提供了最小的 ...

  3. Git教程之使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  4. POJ2531——Network Saboteur(随机化算法水一发)

    Network Saboteur DescriptionA university network is composed of N computers. System administrators g ...

  5. HADOOP基本操作命令

    在这篇文章中,我们默认认为Hadoop环境已经由运维人员配置好直接可以使用. 假设Hadoop的安装目录HADOOP_HOME为/home/admin/hadoop. 启动与关闭 启动HADOOP 进 ...

  6. 优化tomcat——jvm

    Tomcat 的启动参数位于tomcat的安装目录\bin目录下,如果你是Linux操作系统就是catalina.sh文件,如果你是Windows操作系统那么你需要改动的就是catalina.bat文 ...

  7. PHP array_chunk() 函数

    今天在CSDN上,看到了一个问题 一维数组 PHP code   array('0'=>'a',1=>'b',2=>'c',3=>'d',4=>'e',5=>'f' ...

  8. 【HDOJ】2385 Stock

    水题,逆向做+优先级队列. /* 2385 */ #include <iostream> #include <sstream> #include <string> ...

  9. uva 489 Hangman Judge(水题)

    题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  10. 2009国家集训队小Z的袜子

    莫队算法? 感觉没什么优越性啊?难道就是因为在排序的时候cmp函数的不同?这样做为什么减少时限啊? 我带着疑惑敲了代码,却一直有bug…… 代码: type node=record l,r,id,x, ...