使用UIImageView作为属性深度复制Objective-C对象

时间:2012-01-26 12:12:41

标签: objective-c deep-copy

我有一个对象,其属性为NSString,UIImageView和简单的变量,如BOOL,int和float。

我正在尝试创建对象的深层副本。为此,我已经将NSCopying协议实现到我的类和copyWithZone方法,如:

-(id)copyWithZone:(NSZone *)zone
{
    iItem *clone = [[iItem alloc] init];

    //iItem *clone = [super copyWithZone:zone];

    clone.originalTransform = self.originalTransform;
    clone.initialTransform = self.initialTransform;
    clone.originalFrame = self.originalFrame;

    clone.zOrder = self.zOrder;
    clone.itemId = self.itemId;
    clone.itemIdColor = self.itemIdColor;
    clone.itemIdTexture = self.itemIdTexture;
    clone.itemIdStyle = self.itemIdStyle;
    clone.textureLevel = self.textureLevel;
    clone.currentSizePercent = self.currentSizePercent;
    clone.holdItem = self.holdItem;
    clone.isInverted = self.isInverted;
    clone.delegate = self.delegate;
    clone.mergedFrame = self.mergedFrame;

    clone.selectedMergeView = [self.selectedMergeView copy];

    clone.ScaleFactorScreen = self.ScaleFactorScreen;

    clone.selectedFrameView = [self.selectedFrameView copy];
    clone.selectedFrame = [self.selectedFrame copy];
    clone.frameBeforeMovement = self.frameBeforeMovement;

    //clone.touchBeginPoints = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    clone.touchBeginPoints = CFDictionaryCreateMutableCopy(NULL, 0, self.touchBeginPoints);

    clone.contentMode = UIViewContentModeScaleAspectFit;
    clone.animationImages = self.animationImages;
    clone.image = [self.image copy];
    clone.animationDuration = self.animationDuration;
    clone.animationRepeatCount = self.animationRepeatCount;
    [clone startAnimating];


    clone.transform = self.transform;

    clone.frame = self.frame;

    clone.userInteractionEnabled = YES;
    clone.multipleTouchEnabled = YES;

    return clone;
}

类iItem继承自UIImageView类,所以我试着调用super copyWithZone但是我收到了错误。对于我正在尝试克隆的UIImageView对象,我收到SIGABRT消息' - [UIImageView copyWithZone:]:无法识别的选择器发送到实例0x210f50'。

如何制作具有UIImageViews,NSString和简单变量的对象的深层副本?

3 个答案:

答案 0 :(得分:0)

我认为你正在做的一切正确,在实现NSCopying的对象上执行'复制'选择器,并手动设置属性(就像你为图像,内容模式等所做的那样)。

答案 1 :(得分:0)

最简单的方法是使用NSKeyedArchiver进行归档,并且假设您的对象符合NSCoder

,则归档到NSKeyedUnarchiver

答案 2 :(得分:-1)

首先使用此

启动
iItem *clone =[[iItem allocWithZone:zone ] init];