[OC Foundation框架 - 17] copy语法
一个对象使用copy或mutableCopy方法可以创建对象的副本
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
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独立
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对象,再用另一个指向
NSString *str1 = [NSString stringWithFormat:@"abc"];
NSLog(@"%ld", str1.retainCount); // -1
NSString *str2 = [NSString stringWithString:str1];
NSLog(@"%ld", str2.retainCount); //
// 这里不能使用 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 <-- 因为其实拷贝出来的是一个字符串常量
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
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); //
@interface Student : NSObject //copy代表setter会release旧对象,copy新对象
@property (nonatomic, copy) NSString *name; @end
- (id)copyWithZone: (NSZone *) zone
{
Student *stu = [[Student allocWithZone:zone] init];
stu.name = self.name;
return stu;
}
[OC Foundation框架 - 17] copy语法的更多相关文章
- OC Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- OC Foundation框架—结构体
一.基本知识 Foundation—基础框架.框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数据类型,那么包含它的主头文 ...
- OC Foundation框架—字符串
一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...
- iOS - OC Foundation 框架
前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...
- [OC Foundation框架 - 20] 统计代码行数
注意: 1.变量名和函数名不要混淆调用 2.不要对文件夹进行文件的操作,没有权限 3.递归调用注意初始化变量 // // main.m // CodeLineCount // // Created ...
- OC — (Foundation框架-NSDate)
NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间 格林时间 格式化显示时间 ...
- [OC Foundation框架 - 23] 文件管理
A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...
- [OC Foundation框架 - 15] NSDate
日期处理类 定义 时间间隔计算 时间比较,返回其一 时间格式化 void dateCreate() { //date return current time NSDate *date = [NSDat ...
- [OC Foundation框架 - 13] NSValue
NSNumber能够包装基本数据类型称为OC对象,是因为继承了NSValue 包装结构体成OC对象 1.自带结构体 void value() { CGPoint point = CGPointMake ...
随机推荐
- ssh远程执行命令并自动退出(已测试通过)
转自:http://blog.csdn.net/fdipzone/article/details/23000201 ssh命令格式如下: usage: ssh [-1246AaCfgKkMNnqsTt ...
- Centos系统备份与恢复教程
Linux不像windows,它不限制根用户存取任何东西,因此,你完全可以把一个分区上每一个的文件放入一个TAR文件中. 使用root用户切换到根目录 然后,使用下面的命令备份完整的系统: tar c ...
- classpath、path、JAVA_HOME的作用及JAVA环境变量配置
CLASSPATH是什么?它的作用是什么? 它是javac编译器的一个环境变量.它的作用与import.package关键字有关.当你写下improt java.util.*时,编译器面对import ...
- php Ajax 局部刷新
php Ajax 局部刷新: HTML部分 </head> <body> <h1>Ajax动态显示时间</h1> <input type=&quo ...
- 从零开始定义自己的JavaScript框架(一)
来自:http://www.ituring.com.cn/article/48461 1.1 模块的定义 一个框架想要能支撑较大的应用,首先要考虑怎么做模块化.有了内核和模块加载系统,外围的模块就可以 ...
- C#中保留2位小数
public static void Method() { double a = 1.991; a = Math.Round(a); Console.WriteLine("a = {0}&q ...
- chrome插件background.js 和 popup.js 交互
要实现background.js 和 popup.js 之间的交互,首先需要先配置好 manifest.json文件,如: "background":{ //"page& ...
- 1521. War Games 2(线段树解约瑟夫)
1521 根据区间和 来确定第k个数在哪 #include <iostream> #include<cstdio> #include<cstring> #inclu ...
- poj 2635 The Embarrassed Cryptographer(数论)
题目:http://poj.org/problem?id=2635 高精度求模 同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...
- 设计模式 - observer
简单来讲,就是observer依赖于subject,当subject发生变化时,observer得到通知,并将状态与subject同步,常来用于维护对象间状态的一致性. observer的工作过程大体 ...