转载请注明: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. POJ2533Longest Ordered Subsequence(DP)

    http://poj.org/problem?id=2533 在经典不过的DP题目了.... #include <map> #include <set> #include &l ...

  2. user is not in the sudoers file.This incident will be reported

    我用普通用户ssk登陆,想让ssk成为拥有超级用户的权限的普通用户 开始提示输入密码错误 ,然后就这样了   解决方法如下: 1>.进入超级用户模式.也就是输入"su -", ...

  3. jquery操作select 的value和text值

    获取select 的text值: $('#testSelect option:selected').text(); 获取select 的value值: $('#testSelect').val(); ...

  4. Backbone

    app.js作为backbone 业务代码主模块,内容很简单,在页面加载完之后,对AppView进行了实例化

  5. JavaScript寻踪OOP之路

    上一集中,重点介绍了谁动了你的代码.这里先总结一下:咱们的代码从敲下来到运行出结果,经历了两个阶段:分析期与运行期.在分析期,JavaScript分析器悄悄动了我们的代码:在运行期,JavaScrip ...

  6. extjs tablepanel 高度自适应有关问题

    extjs tablepanel 高度自适应问题 项目中为了给客户好点的功能切换体验,想到了用extjs的tabpanel 在页面中用了tabpanel后,高度新打开的tab页的iframe 的高度总 ...

  7. com.sun.image.codec.jpeg--导入报错

    import com.sun.image.codec.jpeg; 这样导入的时候,总是报错:Only a type can be imported. com.sun.image.codec.jpeg ...

  8. Jquery 方式获取 iframe Dom元素

    Jquery 方式获取 iframe Dom元素 測试页面代码: <html>  <head>   <title>jquery方式,訪问iframe页面dom元素& ...

  9. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  10. cdoj 15 Kastenlauf dfs

    Kastenlauf Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/3 De ...