我想创建两种对象,一种是只有“只读”属性的不可变对象,另一种是只有“readwrite”属性的可变对象.
让我们称他们为EXCar和EXMutableCar.
EXCar是NSObject的子类,EXMutableCar是EXCar的子类.
ExCar将拥有其界面
@property (nonatomic,strong,readonly) NSString *name;
EXMutableCar将在其界面中具有
@property (nonatomic,strong) NSString *name;
所以当我使用它的子类EXMutableCar时,我“打开”EXCar的属性.然后它是可变的.
问题是在它们之间正确复制.
我在EXCar中实现了mutableCopyWithZone:
- (id)mutableCopyWithZone:(NSZone *)zone { EXMutableCar *mutableCopy = [[EXMutableCar allocWithZone:zone] init]; mutableCopy.name = _name; return mutableCopy; }
第一个问题,是这样做的好方法吗? (我想要燕子副本)
问题出在copyWithZone上.
由于EXCar的属性是readonly,我既不能在EXCar中创建,也不能在EXMutableCar中创建EXCar的新实例,并填充其属性,如下所示:
- (id)copyWithZone:(NSZone *)zone { EXCar *copy = [[EXCar allocWithZone:zone] init]; copy.name = _name; // This can't work... return copy; }
我真的不想做一个“init”方法,传入15个属性(当然,EXCar就是一个例子,真正的类充满了许多属性).通常它们是从服务器的JSON消息启动的,因此它们不需要复杂的init方法.
第二个问题是这样的,如何做一个使我的类不可变的copyWithZone?
谢谢你的帮助 :)
解决方法
// EXCar.h #import <Foundation/Foundation.h> @interface EXCar : NSObject <NSCopying,NSMutableCopying> @property (nonatomic,readonly) NSString* name; @end
// EXCar.m #import "EXCar.h" #import "EXMutableCar.h" @implementation EXCar - (id)copyWithZone:(NSZone *)zone { EXCar* car = [[[self class] allocWithZone:zone] init]; car->_name = [_name copyWithZone:zone]; return car; } - (id)mutableCopyWithZone:(NSZone *)zone { EXMutableCar* mutableCar = [[EXMutableCar allocWithZone:zone] init]; mutableCar.name = [_name mutableCopyWithZone:zone]; return mutableCar; } @end
// EXMutableCar.h #import "EXCar.h" @interface EXMutableCar : EXCar @property (nonatomic,strong) NSString* name; @end
// EXMutableCar.m #import "EXMutableCar.h" @implementation EXMutableCar @synthesize name = _mutableName; - (id)copyWithZone:(NSZone *)zone { EXMutableCar* car = [super copyWithZone:zone]; car->_mutableName = [_mutableName copyWithZone:zone]; return car; } - (id)mutableCopyWithZone:(NSZone *)zone { EXMutableCar* car = [super mutableCopyWithZone:zone]; car->_mutableName = [_mutableName mutableCopyWithZone:zone]; return car; }
说明:
> EXCar接口实现“复制”协议;
>子类EXMutableCar重写相同的属性,使其成为readwrite.
EXMutableCar实现中的第一件事:手动@synthesize名称,因为Xcode给我们一个警告,因为我们的超类中有相同的属性(但具有不同的访问说明符).
注意我们可以给我们的实例变量赋予相同的名称,比如_name,但重要的是要理解我们在子类中声明一个不同的变量,因为超类中的_name对我们来说是不可访问的.
接下来,Apple文档说明:
If a subclass inherits NSCopying from its superclass and declares additional instance variables,the subclass has to override
copyWithZone:
to properly handle its own instance variables,invoking the superclass’s implementation first.
同样适用于NSMutableCopying:
If a subclass inherits NSMutableCopying from its superclass and declares additional instance variables,the subclass has to override
mutableCopyWithZone:
to properly handle its own instance variables,invoking the superclass’s implementation first.
我们确实声明了其他实例变量,因此我们也在子类中重写这些方法.
结果:
EXCar* car = [[EXCar alloc]init]; // car.name is (null) EXCar* carCopy = [car copy]; // we can do this EXMutableCar* mutableCar = [car mutableCopy]; // and this mutableCar.name = @"BMW"; car = [mutableCar copy]; // car.name is now @"BMW" EXMutableCar* anotherMutableCar = [car mutableCopy]; //anotherMutableCar.name is @"BMW"