oc中的拷贝分为:copy(浅拷贝)和mutablecopy(深拷贝)。

浅拷贝也为指针拷贝,拷贝后原来的对象计数器会+1;

深拷贝为对象拷贝,原来的对象计数器不变。

注意:自定义对象拷贝时要实现NSCoping协议或NSMutableCopying协议.且构造方法和copyWithZone方法中最好用[self class]来代替类名

下面以NSString的拷贝 和Student,DoodStudent的copy(实现NSCoping协议)为例展示:

OC学习基本快告一段落了,终于可以见到IOS界面了呵呵呵呵。。。。。闲话少说直接上代码:

主函数:

//
// main.m
// Copy
//
// Created by WildCat on 13-7-27.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Student.h"
#import "GoodStudent.h"
#pragma mark 练习mutablecopy语法(深拷贝)对象拷贝。
void mutablecopyTest(){ NSString *string =[[NSString alloc] initWithFormat:@"Age is %i",10];
//调用MutableCopy方法,产生一个可变的对象,MutableCopy后的对象和原来的对象不是同一个对象 ,原来的对象和新建的对象的计数器都是1
NSMutableString *str=[string mutableCopy];
[str appendString:@" no is 123"];
NSLog(@"String's:%@ :count is %zi,\n str's: %@: count is %zi",string,[string retainCount],str,[str retainCount]);
[str release]; [string release]; }
#pragma mark copy的练习 copy是浅拷贝(指针拷贝),对象只是做了retain操作
void copyTest(){
NSString *string =[[NSString alloc] initWithFormat:@"Age is %i",10];
//调用Copy方法,产生一个可变的对象,copy后的对象和原来的对象是同一个对象 ,对象的计数器都是+1
NSLog(@"Count:%zi",[string retainCount]);
NSString *str=[string copy];
NSLog(@"Count:%zi",[string retainCount]); NSLog(@"%i",str==string); [str release];
[string release]; }
void studentNameCopy(){ Student *stu=[[[Student alloc] init] autorelease];
NSMutableString *str=[NSMutableString stringWithFormat:@"Jack"];
stu.name=str;
NSLog(@"name is :%@",stu.name);
[str appendString:@" 你好。"];
NSLog(@"name is :%@",stu.name); //因为是用的copy方法 ,所以内容不变。如果用retain name会变
NSLog(@"str is:%@",str); }
void copyStudent(){ Student *stu1=[Student studentWithName:@"lixingle"];
Student *stu2=[stu1 copy];
NSLog(@"Name1 is :%@",stu1.name);
NSLog(@"Name2 is :%@",stu2.name);
stu1.name=@"lele";
NSLog(@"Name1 is :::%@",stu1.name);
NSLog(@"Name2 is :::%@",stu2.name);
[stu2 release]; }
#pragma mark GOOdStudent 的copy练习
void goodStudentCopy(){
GoodStudent *good1=[GoodStudent goodStudentWithAge:10 Name:@"乐乐"];
GoodStudent *good2=[good1 copy]; //改变good1,good2不会变
good1.name=@"长超";
NSLog(@"good1: %@",good1);
NSLog(@"good2: %@",good2);
[good2 release]; } int main(int argc, const char * argv[])
{ @autoreleasepool { //copyTest();
//studentNameCopy();
//copyStudent();
goodStudentCopy(); }
return 0;
}

Student类(例子中父类)

#import <Foundation/Foundation.h>
//对象Copy需要实现NSCoping协议
@interface Student : NSObject<NSCopying>
//NSString 对象一般用copy,外边的内容改变里边的内容不变。retain:当外边的内容改变时里边的内容也会改变
//其他对象建议用retain
@property (nonatomic,copy) NSString *name;
+(id)studentWithName:(NSString *) name;
-(id)copyStudent;
@end
//
// Student.m
// Copy
//
// Created by WildCat on 13-7-27.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import "Student.h" @implementation Student
@synthesize name=_name;
+(id)studentWithName:(NSString *) name{
//这里用[self class] ,方便子类调用
Student *stu=[[[[self class] alloc] init] autorelease];
stu.name=name;
return stu;
} #pragma mark 实现协议
//实现协议,要实现copyWithZone 方法
-(id)copyWithZone:(NSZone *)zone{
//这里创建的对象不需要释放,在外边释放 ;这里用[self class] ,方便子类调用
Student *stu=[[[self class] allocWithZone:zone] init];
stu.name=self.name;
return stu;
}
- (void)dealloc
{
[_name release];
[super dealloc];
}
//重写description方法
-(NSString *)description{
return [NSString stringWithFormat:@"name is %@",self.name]; }
@end

GoodStudent类(子类)

#import "Student.h"

@interface GoodStudent : Student
@property (nonatomic,assign)int age;
+(id) goodStudentWithAge:(int) age Name:(NSString *)name;
@end
#import "GoodStudent.h"

@implementation GoodStudent
+(id) goodStudentWithAge:(int) age Name:(NSString *)name{
GoodStudent *good=[GoodStudent studentWithName:name];
good.age=age;
return good; }
-(id)copyWithZone:(NSZone *)zone{
GoodStudent *copy=[super copyWithZone:zone];
copy.age=self.age;
return copy; }
-(NSString *)description{ return [NSString stringWithFormat:@"name is :%@,age is:%i,",self.name,self.age]; } @end

Object-c学习之路十二(OC的copy)的更多相关文章

  1. zigbee学习之路(十二):zigbee协议原理介绍

    一.前言 从今天开始,我们要正式开始进行zigbee相关的通信实验了,我所使用的协议栈是ZStack 是TI ZStack-CC2530-2.3.0-1.4.0版本,大家也可以从TI的官网上直接下载T ...

  2. IOS学习之路十二(UITableView下拉刷新页面)

    今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...

  3. Java学习之路(十二):IO流<二>

    字符流 字符流是可以直接读写字符的IO流 使用字符流从文件中读取字符的时候,需要先读取到字节数据,让后在转换为字符 使用字符流向文件中写入字符时,需要把字符转为字节在写入文件 Reader和Write ...

  4. 嵌入式Linux驱动学习之路(十二)按键驱动-poll机制

    实现的功能是在读取按键信息的时候,如果没有产生按键,则程序休眠在read函数中,利用poll机制,可以在没有退出的情况下让程序自动退出. 下面的程序就是在读取按键信息的时候,如果5000ms内没有按键 ...

  5. Java学习之路(十二):IO流<三>

    复习:序列流 序列流可以把多个字节输入整合成一个,从序列流中读取到数据时,将从被整合的第一个流开始读取,读完这个后,然后开始读取第二个流,依次向后推. 详细见上一篇文章 ByteArrayOutput ...

  6. Java学习之路(十二):IO流

    IO流的概述及其分类 IO流用来处理设备之间的数据传输,Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流(读写数据)     输出流(写数据) 流按操作 ...

  7. java痛苦学习之路[十二]JSON+ajax+Servlet JSON数据转换和传递

    1.首先client须要引入 jquery-1.11.1.js 2.其次javawebproject里面须要引入jar包  [commons-beanutils-1.8.0.jar.commons-c ...

  8. FastAPI 学习之路(二十)接口文档配置相关

    系列文章: FastAPI 学习之路(一)fastapi--高性能web开发框架 FastAPI 学习之路(二) FastAPI 学习之路(三) FastAPI 学习之路(四) FastAPI 学习之 ...

  9. VSTO 学习笔记(十二)自定义公式与Ribbon

    原文:VSTO 学习笔记(十二)自定义公式与Ribbon 这几天工作中在开发一个Excel插件,包含自定义公式,根据条件从数据库中查询结果.这次我们来做一个简单的测试,达到类似的目的. 即在Excel ...

随机推荐

  1. 五、SolrJ、Request Handler

    什么是SolrJ 既然Solr是以单独的WebApp形式存在的,那么Solr理应提供与Solr通信的Api吧,对的,这就是SolrJ,既然与solr通信是通过url,那么其实我们也可以不用SolrJ, ...

  2. [iOS]使用symbolicatecrash分析crash文件

    对于我们iOS开发者来说,最心碎的事莫过于苹果审核一个星期后上架app store,而第二天就报出闪退bug.一周前我刚经历过,而且最坑的是由于第一次做个人开发,经验不足,没有集成友盟的分析SDK,还 ...

  3. Android 屏幕适配方案

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45460089: 本文出自:[张鸿洋的博客] 1.概述 大家在Android开发 ...

  4. mybati之#与$的区别

    $是用于sql的拼接: //因为user_name是String类型,所以在sql中加上单引号,需要手动的判断数据类型,value是如果没有指定参数的话,value就是默认参数名称,获取穿的参数就是: ...

  5. 1:scrapy框架原理与环境搭设

    1:原理图: (*此图来自网络) 2:开发过程: 1)编写items.py,确定要抓取的关键字段名称 2)编写spider,确定发送request的形式以及对于response的处理 3)编写pipe ...

  6. 递归生成树对象,应用于Easyui,Tree控件

    1.生成树节点对象 /// <summary> /// 生成树的节点 /// </summary> public class TreeNode { public TreeNod ...

  7. 《Spring敲门砖之基础教程第一季》 第一章 概要介绍

    百度百科say: Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建.简单来说,Spring是一个分层的JavaSE/EEful ...

  8. shell中的eval

    eval语法 eval arg1 arg2 ... eval的作用就是将后面的参数arg1 arg2等等当成一个pipeline,然后重新执行shell处理pipeline的流程(有关pipeline ...

  9. 使用APMServ本地搭建多个网站

    October 27, 2014 使用APMServ本地搭建多个网站教程 把我写好的代码直接粘贴到 httpd.conf 文件的末尾.然后保存就可以了.代码如下: <VirtualHost *: ...

  10. Factorial Solved Problem code: FCTRL

    import sys #import psyco #很奇怪,这题用psyco就runtime error #psyco.full() def z(n): #这个应该是技巧的一种算法 r = 0 whi ...