ios – 如何正确实现mutableCopyWithZone和copyWithZone

ios – 如何正确实现mutableCopyWithZone和copyWithZone,第1张

概述我读了很多关于它的话题,但我还是迷路了. 我想创建两种对象,一种是只有“只读”属性的不可变对象,另一种是只有“readwrite”属性的可变对象. 让我们称他们为EXCar和EXMutableCar. EXCar是NSObject的子类,EXMutableCar是EXCar的子类. ExCar将拥有其界面 @property (nonatomic, strong, readonly) NSStri 我读了很多关于它的话题,但我还是迷路了.

我想创建两种对象,一种是只有“只读”属性的不可变对象,另一种是只有“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 thisEXMutableCar* mutableCar = [car mutablecopy]; // and thismutableCar.name = @"BMW";car = [mutableCar copy]; // car.name is Now @"BMW"EXMutableCar* anotherMutableCar = [car mutablecopy]; //anotherMutableCar.name is @"BMW"
总结

以上是内存溢出为你收集整理的ios – 如何正确实现mutableCopyWithZone和copyWithZone全部内容,希望文章能够帮你解决ios – 如何正确实现mutableCopyWithZone和copyWithZone所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存