黑马程序员-Object C之copy和mutableCopy语法(二)
来源:互联网 发布:js 上传与下载文件 编辑:程序博客网 时间:2024/06/06 03:03
------------------------------ASP.Net+Unity开发、.Net培训、期待与您交流!---------------------------
自定义对象的拷贝
首先,要知道在Object C中,并不是所有的对象都支持copy和mutableCopy。只有遵守NSCopying协议的类可以发送copy方法,遵守NSMutableCopying协议的类才可以发送mutableCopy方法。要实现自己定义对象的拷贝,需要遵守NSCopying或者NSMutableCopying协议,并且要实现copyWithZone:或mutableCopyWithZone:方法。
下面代码展示自定义对象的拷贝:
1.定义两个类
(1)Student类的声明:
@interface Student : NSObject <NSCopying>// copy代表set方法会release旧对象、copy新对象// 修改外面的变量,并不会影响到内部的成员变量// 建议:NSString一般用copy策略,其他对象一般用retain@property (nonatomic, copy) NSString *name;+ (id)studentWithName:(NSString *)name;@endStudent类的实现:
#import "Student.h"@implementation Student+ (id)studentWithName:(NSString *)name { // 这里最好写[self class],当子类初始化名字时返回子类对象 Student *stu = [[[[self class] alloc] init] autorelease]; stu.name = name;//初始化姓名 return stu;}- (void)dealloc { [_name release]; [super dealloc];}#pragma mark description方法内部不能打印self,不然会造成死循环- (NSString *)description { return [NSString stringWithFormat:@"[name=%@]", _name];}#pragma mark copying协议的方法// 这里创建的副本对象不要求释放- (id)copyWithZone:(NSZone *)zone { Student *copy = [[[self class] allocWithZone:zone] init]; // 拷贝名字给副本对象 copy.name = self.name; return copy;}@end(2)GoodStudent类,为Student的子类
@interface GoodStudent : Student@property (nonatomic, assign) int age;+ (id)goodStudentWithAge:(int)age name:(NSString *)name;@endGoodStudent类的实现:
#import "GoodStudent.h"@implementation GoodStudent+ (id)goodStudentWithAge:(int)age name:(NSString *)name { GoodStudent *good = [GoodStudent studentWithName:name]; good.age = age;//初始化年您 return good;}- (NSString *)description { return [NSString stringWithFormat:@"[name=%@, age=%i]", self.name, _age];}- (id)copyWithZone:(NSZone *)zone { // 一定要调用父类的方法 GoodStudent *copy = [super copyWithZone:zone]; copy.age = self.age; //拷贝年您给副本对象 return copy;}@end测试代码:
#pragma mark 演示Student的name的copyvoid studentNameCopy() { Student *stu = [[[Student alloc] init] autorelease]; NSMutableString *string = [NSMutableString stringWithFormat:@"age is %i", 10]; stu.name = string; [string appendString:@"abcd"]; //不会影响name的改变 NSLog(@"name=%@", stu.name); NSLog(@"string=%@", string);}#pragma mark 演示Student的copyvoid studentCopy() { Student *stu1 = [Student studentWithName:@"stu1"]; Student *stu2 = [stu1 copy];//对象的copy stu2.name = @"stu2"; NSLog(@"stu1:%@", stu1); NSLog(@"stu2:%@", stu2); [stu2 release];}void goodStudentCopy() { GoodStudent *stu1 = [GoodStudent goodStudentWithAge:10 name:@"good1"]; GoodStudent *stu2 = [stu1 copy]; stu2.name = @"good2"; stu2.age = 11; NSLog(@"stu1:%@", stu1); NSLog(@"stu2:%@", stu2);}
-----------------------------ASP.Net+Unity开发、.Net培训、期待与您交流!-------------------------
详细请查看:www.itheima.com
0 0
- 黑马程序员-Object C之copy和mutableCopy语法(二)
- 黑马程序员-Object C之copy与mutableCopy语法(一)
- Objective-C之语法copy、mutablecopy以及自定义复制功能
- 【黑马程序员】copy与mutableCopy实例
- OC语法——Object-C retain、copy、mutableCopy的详细分析
- Objective-c的copy和mutableCopy
- Object-C对象复制:copy与mutableCopy、NScopying与NSMutableCopy
- 黑马程序员-OC笔记(十)copy-mutableCopy
- 黑马程序员——IOS学习之二:Objective-C基础语法和关键字——黑马 ios 技术博客
- Objective-C之 深拷贝和浅拷贝、copy和mutableCopy(可以验证下)
- copy和mutableCopy
- iphone copy 和mutablecopy
- copy和mutablecopy浅见
- copy和mutablecopy
- copy和mutablecopy
- copy和mutablecopy
- copy和mutableCopy
- Copy和MutableCopy
- 分布式缓存memcached原理
- 网站性能优化 - 数据库及服务器架构篇
- 2.4.0版 hadoop伪分布式模式在mac上的配置和运行
- Linux查看系统信息的一些命令及查看已安装软件包的命令
- pkg-config详解
- 黑马程序员-Object C之copy和mutableCopy语法(二)
- 网页宽高自适应大小(转)
- Servlet 返回登录前页面
- 《人工智能的未来》读书笔记
- ubuntu安装配置ftp服务
- iOS宏编译的两种方法
- 电商实训二:电子支付工具
- Socket超时设置---select()的妙用
- [顶] 史上最经典的IT入门学习教程DVD光盘免费领!