Objective-C基础回顾 1. 复制--NSCopying
来源:互联网 发布:雄3导弹知乎 编辑:程序博客网 时间:2024/05/21 06:38
让我们复习一下内存管理的规则:“如果你使用alloc、copy或new方法获取了一个对象,则该对象的保留计数器的值为1,而且你要释放它。”既然叫copy方法,肯定就能创建对象的副本。copy消息会告诉对象创建一个全新的对象,并让新对象与接收copy消息的原对象完全一样。
一个对象如果想调用copy方法,需要实现NSCopying协议,该协议如下:
The NSCopying protocol declares a method for providing functional copies of an object. The exact meaning of “copy” can vary from class to class, but a copy must be a functionally independent object with values identical to the original at the time the copy was made. A copy produced with NSCopying is implicitly retained by the sender, who is responsible for releasing it.
NSCopying declares one method, copyWithZone:, but copying is commonly invoked with the convenience methodcopy. Thecopy method is defined for all objects inheriting fromNSObject and simply invokescopyWithZone: with the default zone.
Your options for implementing this protocol are as follows:
- Implement NSCopying using alloc and init... in classes that don’t inheritcopyWithZone:.
- Implement NSCopying by invoking the superclass’scopyWithZone: whenNSCopying behavior is inherited. If the superclass implementation might use theNSCopyObject function, make explicit assignments to pointer instance variables for retained objects.
- Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.
If a subclass inherits NSCopying from its superclass and declares additional instance variables, the subclass has to overridecopyWithZone: to properly handle its own instance variables, invoking the superclass’s implementation first.
下面通过一个例子来说明:
定义一个Cat类,初始化一个对象cat1,然后通过cat1调用copy方法来创建新的对象cat2
Cat.h文件
#import <Foundation/Foundation.h>@interface Cat : NSObject<NSCopying>{ NSString *_name; //名称 NSString *_color; //颜色};- (void) setName:(NSString*) name andColor:(NSString *) color;- (NSString *) getName;- (NSString *) getColor;@end
Cat.m文件
#import "Cat.h"@implementation Cat- (void) setName:(NSString*) name andColor:(NSString *) color{ _name = name; _color = color;}- (NSString *) getName{ return _name;}- (NSString *) getColor{ return _color;}- (id) copyWithZone:(NSZone *)zone{ Cat *newCat = [[Cat allocWithZone:zone] init]; [newCat setName:_name andColor:_color]; return newCat;}@end
main.m文件
#import <Foundation/Foundation.h>#import "Cat.h"int main(int argc, const char * argv[]){ @autoreleasepool { //通过alloc初始化创建对象 Cat *cat1 = [[Cat alloc] init]; [cat1 setName:@"Mimi" andColor:@"red"]; //通过copy方法创建新对象cat2 Cat *cat2 = [cat1 copy]; [cat2 setName:@"JJ" andColor:@"blue"]; NSLog(@"cat1 %@ %@ %@ ", cat1, [cat1 getName], [cat1 getColor]); NSLog(@"cat2 %@ %@ %@ ", cat2, [cat2 getName], [cat2 getColor]); } return 0;}
输出结果:
cat1 <Cat: 0x10010b5e0> Mimi red
cat2 <Cat: 0x10010bc30> JJ blue
- Objective-C基础回顾 1. 复制--NSCopying
- Objective-C深复制和<NSCopying>协议
- Objective-C 深复制和浅复制与<NSCopying>协议
- Objective-c之NSCopying
- Effetive Objective - C 1.Objective 22.理解NSCopying协议
- Objective-c的NSCopying协议
- Objective-C的NSCopying协议
- Effective Objective-C 2.0: Item 22: Understand the NSCopying Protocol
- [iOS]Objective-C基础回顾:继承和委托
- Objective-C基础回顾 2. 委托方法--delegate
- Object-C对象复制:copy与mutableCopy、NScopying与NSMutableCopy
- objective-c 复制对象
- objective-c复制
- Objective-C复制解析
- 1.Objective-C基础学习
- OC基础回顾(一)Objective-C是对C语言的扩展
- Objective-c下的深拷贝、浅拷贝以及NSCopying协议
- bruceyou1990-iOS学习日记-Objective-C深拷贝浅拷贝和NSCopying协议
- Java中泛型(1)——泛型介绍(传智播客毕老师视频讲解)
- 解密腾讯SNG云运维平台“织云”
- 程序员应该常去的一些网站
- 最简单也最难——如何获取到Android控件的高度
- Lucene 4.4 IndexWriter构造函数中不提供是否增量的参数。
- Objective-C基础回顾 1. 复制--NSCopying
- kafka
- iphone-命令行编译之--xcodebuild
- Android ActionBar完全解析,使用官方推荐的最佳导航栏(上)
- centos yum 库更新
- atof函数的实现
- 30多年程序员生涯经验总结
- 实现XML的简单动态配置
- Atlas源码学习(五)