设为首页 加入收藏

TOP

Objective-C的NSCopying协议
2014-11-23 21:31:40 来源: 作者: 【 】 浏览:13
Tags:Objective-C NSCopying 协议

在Objective-c中, 某个类遵守了NSCopying协议就代表这个类支持[obj copy]操作。在没有实现NSCopying协议的情况下调用对象的copy函数则会出现异常。NSCopying协议只有一个函数,即copyWithZone, 声明如下 :


@protocol NSCopying


- (id)copyWithZone:(NSZone *)zone;


@end


zone参数是新对象将分配到的内存区,一般不用修改。


遵守NSCopying协议则需要在@implementation中实现copyWithZone函数。



// Person
@interface Person : NSObject



@property (nonatomic, copy) NSString* name;
@property int age ;



-(id) initWithName:(NSString*) pName andAge:(int) iAge ;
@end


Person类的实现。



// impl
@implementation Person



@synthesize name, age ;



//
-(id) initWithName:(NSString*) pName andAge:(int) iAge
{
self = [super init] ;
if ( self ) {
self.name = pName ;
self.age = iAge ;
}
return self ;
}



// 实现copyWithZone
-(id) copyWithZone:(NSZone *)zone
{
NSLog(@"Person copyWithZone, class : %@.", [self.class description]) ;
// 如果子类覆写该方法, 则[self class]的类型为子类型,如果直接使用
// [[Person allocWithZone:zone] init], 则子类覆写时会出错.
Person* per = [[[self class] allocWithZone:zone] init] ;
per.name = self.name ;
per.age = self.age ;
return per ;
}



@end


// impl
@implementation Person


@synthesize name, age ;


//
-(id) initWithName:(NSString*) pName andAge:(int) iAge
{
self = [super init] ;
if ( self ) {
self.name = pName ;
self.age = iAge ;
}
return self ;
}


// 实现copyWithZone
-(id) copyWithZone:(NSZone *)zone
{
NSLog(@"Person copyWithZone, class : %@.", [self.class description]) ;
// 如果子类覆写该方法, 则[self class]的类型为子类型,如果直接使用
// [[Person allocWithZone:zone] init], 则子类覆写时会出错.
Person* per = [[[self class] allocWithZone:zone] init] ;
per.name = self.name ;
per.age = self.age ;
return per ;
}


@end


在copyWithZone函数中创建了一个对象,并且将属性挨个拷贝给新的对象。注意新对象per的创建方法


[[[self class] allocWithZone:zone] init] ;


而不是


[[Person allocWithZone:zone] init] ;


这是因为如果有子类覆写了copyWithZone函数,那么在子类对象被拷贝时会调用Person类的copyWithZone函数, 如果硬编码类型为Person, 则创建的对象为Person类型, 而不是子类型,因此会出现异常。而[self class]则会获取正确的类型, 并且创建出正确的对象,避免异常出现。


例如Student类继承自Person类。


// subclass, Student
@interface Student : Person
// 班级名
@property (nonatomic, copy) NSString* className ;


@end


// subclass, Student
@interface Student : Person
// 班级名
@property (nonatomic, copy) NSString* className ;


@end


Student的实现,覆写copyWithZone函数。


// Student实现
@implementation Student


// 覆写copyWithZone,
-(id) copyWithZone:(NSZone *)zone
{
// 1、先调用父类的copyWithZone函数
Student* per = [super copyWithZone:zone] ;
// 2、再设置className
per.className = self.className ;
NSLog(@"Student copyWithZone") ;
return per ;
}


@end





】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Objective-C中的Block(块)详解 下一篇Objective-C中通过分类来扩展现有..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: