复制自定义对象 [英] copying a custom object

查看:82
本文介绍了复制自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Layer的对象,它有一些属性和一些方法。

I have an object called Layer that has some attributes and some methodes.

我需要将Layer传递给第二个视图控制器:

i need to pass Layer to a second view controller:

SecondVC *view = [self.storyboard instantiateViewControllerWithIdentifier:@"2VC"];
view.Layer = [[Layer alloc] initWithMapLayer:self.Layer];
view.delegate = self;

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:navController animated:YES];

,我可能会更改属性。然后我通过委托返回Layer对象;

in the SecondVC, i may change the attributes. I then return the Layer object back via a delegate;

-(void)done
{
    [self.delegate returnLayer:self.layer];

    [self dismissModalViewControllerAnimated:YES];
}

现在我的问题是我将指针传递给我的第一个视图控制器的图层对象,当我更新第二个视图控制器中的图层时,我的第一个视图控制器的图层对象也在更新中。

now my problem is that i am passing a pointer to my first view controller's Layer object and when i update the Layer in the second view controller, my first view controller's Layer object is also being updated.

因为这个,我无法判断它是否已被更改(如果有,我需要运行一些代码)。

because of this, i can't tell if it has been changed (if it has, i need to run some code).

如何创建Layer对象的副本并将其传递给我的第一个视图控制器的Layer对象而不是指针?

how can i create a copy of my Layer object and pass that instead of a pointer to my first view controller's Layer object?

编辑:

我尝试使用第二种初始化方法:

i did try use a second init method:

-(id)initWithLayer:(Layer *)Layer
{
    if (self = [super init])
    {
        self.call = [[FunctionCall alloc] init];        
        self.HUD = [[MBProgressHUD alloc] init];

        self.Layers = [[NSMutableDictionary dictionaryWithDictionary:Layer.Layers] copy];
        self.nameList = [[NSArray arrayWithArray:Layer.nameList] copy];
    }
    return self;
}

无效。

EDIT2:

尝试

Layer *copyLayer = [self.myLayer copy];

layerView.myLayer = copyLayer;

并收到错误

-[layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40
2012-06-12 11:15:28.584 Landscout[8866:1fb07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Layer copyWithZone:]: unrecognized selector sent to instance 0xfc72c40'

已解决:

我在initWithLayer方法中添加了一个深层副本:

i added a deep copy to the initWithLayer method:

for (id key in layer.layers)
{
    [newLayers setValue:[[layer.layers objectForKey:key] mutableCopy] forKey:[key mutableCopy]];
}

for (id name in layer.nameList)
{
    [newList addObject:[name mutableCopy]];
}

这给了我一个Layer对象的副本

this gives me a copy of the Layer object

推荐答案

您需要将复制函数实现到您的对象

You will need to implement copy function to your object

在你的Layer.m

In your Layer.m

- (id)copy
{
    Layer *layerCopy = [[Layer alloc] init];

    //YOu may need to copy these values too, this is a shallow copy
    //If YourValues and someOtherValue are only primitives then this would be ok
    //If they are objects you will need to implement copy to these objects too
    layerCopy.YourValues = self.YourValues;
    layerCopy.someOtherValue = self.someOtherValue;

    return layerCopy;
}

现在在您的通话功能中

//instead of passing self.Layer pass [self.Layer copy]
view.Layer = [[Layer alloc] initWithMapLayer:[self.Layer copy]];

这篇关于复制自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆