转载请注明:http://www.cnblogs.com/letougaozao/p/3631105.html

  • 概念
  • 内存管理
  • NSString的copy实例
  • 对象的copy实例

一、概念

目的:在改变原有对象的时候,不会改变新对象的值

  1. Copy:实现NSCopying协议,创建的是一个不可变副本
  2. MutableCopy:实现NSMutableCopying协议,创建的是一个可变副本

二、内存管理

  1. 深拷贝:产生新的对象,所以源对象计数器不变>>>对象拷贝
  2. 浅拷贝:不产生新对象,所以源对象计数器加一>>>指针拷贝(因为对象本身不可以变,所有没有必要再创建一个对象)

三、NSString的copy实例

//
// main.m
// Copy语法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h> void test1()
{
NSString *str = [NSString stringWithFormat:@"age is %i", ];
NSString *str1 = [str copy]; NSLog(@"%i", str == str1); NSString *str2 = [str mutableCopy]; NSLog(@"%i", str2 == str);
} void test2()
{
NSMutableString *str = [NSMutableString stringWithFormat:@"age is %i", ]; NSString *str1 = [str copy];
NSMutableString *str2 = [str mutableCopy]; [str appendFormat:@""]; NSLog(@"%i", str == str2);
NSLog(@"%i", str == str1);
NSLog(@"%@", str);
NSLog(@"%@", str1);
} int main(int argc, const char * argv[])
{ @autoreleasepool { test2(); }
return ;
}

四、对象拷贝的实例

对象的拷贝,主要注意点

  1. 必须实现NSCopying协议
  2. 需要重写- (id)copyWithZone:(NSZone *)zone方法
  3. 代码中 self class的引用

1⃣️GoodStudent.h

//
// GoodStudent.h
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "Student.h" @interface GoodStudent : Student @property (nonatomic, assign) int age; +(id)goodStudentWithName:(NSString *)name withAge:(int)age; @end

2⃣️GoodStudent.m

//
// GoodStudent.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "GoodStudent.h" @implementation GoodStudent +(id)goodStudentWithName:(NSString *)name withAge:(int)age
{
GoodStudent *stu = [super studentWithName:name];
stu.age = age; return stu;
} -(id)copyWithZone:(NSZone *)zone
{
GoodStudent *copy = [super copyWithZone:zone];
copy.age = self.age; return copy;
} -(NSString *)description
{
return [NSString stringWithFormat:@"%@-%i", self.name, self.age];
} @end

3⃣️Student.h

//
// Student.h
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h> @interface Student : NSObject <NSCopying> @property (nonatomic, copy) NSString *name; +(id)studentWithName:(NSString*)name; @end

4⃣️Student.m

//
// Student.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import "Student.h" @implementation Student
+(id)studentWithName:(NSString *)name
{
Student *stu = [[[[self class] alloc] init] autorelease];
stu.name = name; return stu;
} - (id)copyWithZone:(NSZone *)zone
{
Student *copy = [[self class] allocWithZone:zone]; copy.name = self.name; return copy;
} -(NSString *)description
{
return [NSString stringWithFormat:@"%@", self.name];
} -(void)dealloc
{
[_name release];
[super dealloc];
}
@end

main.m

//
// main.m
// Student的Copy用法
//
// Created by apple on 14-3-28.
// Copyright (c) 2014年 apple. All rights reserved.
// #import <Foundation/Foundation.h>
#import "GoodStudent.h" void test1()
{
Student *stu = [Student studentWithName:@"name1"];
Student *stu1 = [stu copy]; NSLog(@"%@", stu);
NSLog(@"%@", stu1);
} void test2()
{
GoodStudent *stu1 = [GoodStudent goodStudentWithName:@"name1" withAge:]; GoodStudent *stu2 = [stu1 copy]; NSLog(@"%@", stu1);
NSLog(@"%@", stu2); } int main(int argc, const char * argv[])
{ @autoreleasepool { test2(); }
return ;
}

OC之Copy语法的更多相关文章

  1. copy语法

    copy 和 mutableCopy 一个对象使用copy或者mutableCopy方法可以创建对象的副本 --------------- copy - 需要先实现NSCopying协议,创建的是不可 ...

  2. OC基础 点语法的使用

    OC基础 点语法的使用 1.创建一个Student类继承于NSObject,Student.h文件 #import <Foundation/Foundation.h> @interface ...

  3. 「OC」点语法和成员变量的作用域

    一.点语法 (一)认识点语法 声明一个Person类: 1 #import <Foundation/Foundation.h> 2 3 @interface Person : NSObje ...

  4. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法

    一. 分类-Category 1. 基本用途:Category  分类是OC特有的语言,依赖于类. ➢ 如何在不改变原来类模型的前提下,给类扩充一些方法?有2种方式 ● 继承 ● 分类(Categor ...

  5. 【Swfit】Swift与OC两种语法写单例的区别

    Swift与OC两种语法写单例的区别 例如写一个NetworkTools的单例 (1)OC写单例 + (instancetype)sharedNetworkTools { static id inst ...

  6. [OC Foundation框架 - 17] copy语法

    一个对象使用copy或mutableCopy方法可以创建对象的副本 1.copy 需要实现NSCopying协议 创建出来的是不可变副本,如NSString, NSArray, NSDictionar ...

  7. OC:Block语法、Block使用、Block实现数组排序

    Block //定义一个求两个数最大值函数 int maxValue (int ,int); //函数的实现 int maxValue (int a, int b){ return  a > b ...

  8. Object-c学习之路十二(OC的copy)

    oc中的拷贝分为:copy(浅拷贝)和mutablecopy(深拷贝). 浅拷贝也为指针拷贝,拷贝后原来的对象计数器会+1: 深拷贝为对象拷贝,原来的对象计数器不变. 注意:自定义对象拷贝时要实现NS ...

  9. 黑马程序员——OC语言基础语法 面向对象的思想

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结)(一)基础语法 1)关键字 @interface.@implementati ...

随机推荐

  1. flask中的request对象方法

    'accept_charsets','accept_encodings','accept_languages','accept_mimetypes','access_route','applicati ...

  2. 使用SQLCOMMAND以及SQLADAPERT 调用存储过程

    使用SQLCommand调用的基本方法如下: SqlCommand comm = new SqlCommand("P_GetCompanyInfo", conn); comm.Co ...

  3. emWin5.24 VS2008模拟LCD12864 stm32 RTX移植 【worldsing笔记】

      emWin for 12864 并口移植 源代码下载:RTX_emWin5.24_Keil_VS2008-20141122.zip   硬件环境: CPU: stm32f103ve LCD:st7 ...

  4. RS232串口用事件接受数据(一问一答)

    private void button1_Click(object sender, EventArgs e) { serialPort1.Open(); serialPort1.DataReceive ...

  5. hdoj 5399 Tpp simple

    WA了一下午.... 1WA:T了,因为阶乘没打表所以时间超了.. 2WA,3WA:runtime error,检查的value数组开小了,应该是MAXN.. 4WA.5WA.6WA:改了改对cnt的 ...

  6. CSS Hack技术(一)

    这世间坑爹的东西不少,浏览器可以算做一件,尤其的IE浏览器.关于浏览器的吐槽已经有不少了,我也就不在这添油加醋了.不过吐槽终究只是泄一时之愤,解决问题才是关键,今天我们就来讲一讲浏览器(样式)兼容的技 ...

  7. c++中从一段字符串中提取数字

    采用标准输入输出: 输入:12&3 34*133^3131   13031* 输出:12 3 34 133 3131 13031 思路,先将整个输入存进一个字符串,再解析字符串,这样运行速度会 ...

  8. Java组各任务工作流程

    1.周枫 A.提供基于SQL SERVER的数据库基本表结构创建脚本,基础数据脚本,按学科(产品)的数据脚本. 2.吴缤 A.提供给周茉的安装包用的项目文件,共三个digital,xylinkWeb和 ...

  9. vimrc常用配置项

    设置行号 set nu 设置自动缩进 set autoindent 设置tab占n个字符 set tabstop=n 设置以空格代替tab(因为有部分场合不允许使用tab) set expandtab ...

  10. android学习记录(十三)Task 和 Activity 回退栈操作。

    首先说一下Task是一个什么概念吧:Task是一个包括activity的列表.没 错.简单的说就是依照启动的先后来排队的一个队列.Back Stack.就是回退栈的意思:那么有什么用?Back Sta ...