转载请注明: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. Codeforces 118 D. Caesar's Legions (dp)

    题目链接:http://codeforces.com/contest/118/problem/D 有n个步兵和m个骑兵要排成一排,其中连续的步兵不能超过k1个,连续的骑兵不能超过k2个. dp[i][ ...

  2. UVa11997K Smallest Sums(优先队列)

    K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly ...

  3. 将UIImage保存成JPG或PNG格式存储在本地

    -(void)pngAndJpg:(UIImage*)image{ NSString *pngPath = [NSHomeDirectory() stringByAppendingPathCompon ...

  4. Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol

    - (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...

  5. CSS学习篇核心之——盒子模型

    概述 关于CSS的一些基础知识我们在前面文章中已经有所了解,这篇文章我们主要来学习下CSS中的核心知识盒子模型的知识. 盒子模型 元素框的最内部分是实际的内容(element),直接包围内容的是内边距 ...

  6. IIS7/8 HTTP Error 500.19 错误 0x80070021

    IIS7.0/8.0的错误HTTP Error 500.19 - Internal Server Error ,错误代码为0x80070021,大概原因为IIS7.0的安全设定相比前版本有很大的变更. ...

  7. Flex XML

    一.创建XML类 1.把字符串对象转换为XML: var xmlStr:String="<students><student><name>吕布</n ...

  8. php获取GET方式传入的全部变量名称与值:foreach用法

    $count = count($_GET); $i = 0; foreach ($_GET as $key => $value) { if ($i == $count - 1) { $str . ...

  9. Ehcache(07)——Ehcache对并发的支持

    http://haohaoxuexi.iteye.com/blog/2119733 Ehcache对并发的支持 在高并发的情况下,使用Ehcache缓存时,由于并发的读与写,我们读的数据有可能是错误的 ...

  10. 用jQuery解决弹出层的问题

    在BS 项目中 经常需要用到这种弹出层.做这种弹出层一般都会遇到下面几个问题:0,弹出层必须定义在input的下边显示.1,点击input弹出div层.2,点击div层外面任何地方,关闭div层.3, ...