iPhone:复制/克隆

发布于 2024-12-27 00:54:21 字数 112 浏览 4 评论 0原文

我想复制或克隆我自己编写的类的对象。 但如果我调用复制函数,则仅复制指针。因此,如果我更改复制的对象,原始对象也会更改。

有没有一种方法/功能可以真正克隆一个对象?

此致 梅兰妮

I want to copy or clone a object of my own written class.
But if I call the copy function only the pointer were copied. So if I change the copied object the original object is changed, too.

Is there a way/function to really clone an object?

best regards
Melanie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2025-01-03 00:54:22

您所追求的被称为“深层复制”,您想要复制指针的内容,而不是它指向的地址。

下面有一些关于此列表的堆栈溢出问题:

另外,这里还有一个来自 Techtopia 的文章:此处

此处添加是我的 Google 搜索:这里

What you are after is refered to as Deep Copy, where you want to copy the contents of a pointer, not the address it points to.

There are a few stack overflow questions about this list Below:

Additionally here is a article from Techtopia: Here

Addition here is my Google Search: Here

唐婉 2025-01-03 00:54:21

如果对象的类采用 NSCopying 协议并实现其单一方法,则可以复制对象 copyWithZone:

请参阅对象复制


- (id)copyWithZone:(NSZone *)zone{
    MyClass *copy = [[[self class] allocWithZone: zone] init];
    [copy setProperty1:[self property1]];
    return copy;
}

An object can be copied if its class adopts the NSCopying protocol and implements its single method, copyWithZone:.

See Object copying


- (id)copyWithZone:(NSZone *)zone{
    MyClass *copy = [[[self class] allocWithZone: zone] init];
    [copy setProperty1:[self property1]];
    return copy;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文