mutableCopyWithZone 更新属性值
我有一个类需要复制,并且能够更改两个类上变量的值。简而言之,这些类需要始终保持彼此的克隆。我对文档的理解是,我可以使用也被声明为可变的类的浅拷贝来完成此操作。通过浅复制,变量的指针值将被克隆,以便它在两个类中完全匹配。因此,当我更新原始变量时,副本将同时更新。这是对的吗?
正如您在下面看到的,我在要复制的类中使用了 mutableCopyWithZone 。我已经尝试了 NSCopyObject 和 allocWithZone 方法来使其工作。尽管我能够复制该类并且它按预期显示,但在更新变量时,它并没有更改复制的类中的值。
- (id)mutableCopyWithZone:(NSZone *)zone {
//ReviewViewer *copy = NSCopyObject(self, 0, zone);
ReviewViewer *copy = [[[self class] allocWithZone:zone] init];
copy->infoTextViews = [infoTextViews copy];
return copy;
}
infoTextViews 是一个声明为非原子的属性,保留在被复制的类的头文件中。我还相应地实现了 NSMutableCopying 协议。
任何帮助都会很棒。
I have a Class that I need to copy with the ability to make changes the value of a variable on both Classes. Simply put these classes need to remain clones of each other at all times. My understanding of the documentation is that I can do this using a shallow copy of the Class which has also been declared mutable. By shallow copying the pointer value for the variable will be cloned so that it is an exact match in both classes. So when I update the variable in the original the copy will be updated simultaneously. Is this right?
As you can see below I have used mutableCopyWithZone in the class I want to copy. I have tried both NSCopyObject and allocWithZone methods to get this to work. Although I'm able to copy the class and it appears as intended, when updating the variable it is not changing value in the copied Class.
- (id)mutableCopyWithZone:(NSZone *)zone {
//ReviewViewer *copy = NSCopyObject(self, 0, zone);
ReviewViewer *copy = [[[self class] allocWithZone:zone] init];
copy->infoTextViews = [infoTextViews copy];
return copy;
}
infoTextViews is a property declared as nonatomic, retain in the header file of the class being copied. I have also implemented the NSMutableCopying protocol accordingly.
Any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,你想要的是浅复制,但你做的是深复制。将[infoTextViews copy]更改为[infoTextViewsretain]
小点..allocWithZone?您的意思是 allocWithZone:zone 吗?普通的旧分配可能没问题。
为什么是 mutableCopyWithZone: ? ReviewViewer 是否有可变和不可变版本?您可能只需要 copyWithZone:
注意:如果您重写 copyWithZone 来执行浅复制,则您将在复制对象的任何地方指定此行为。
You are right, what you want is a shallow copy, but what you do is a deep copy. Change [infoTextViews copy] to [infoTextViews retain]
Small points.. allocWithZone? You mean allocWithZone:zone ? Plain old alloc would probably be fine.
Why mutableCopyWithZone: ? Are there mutable and immutable versions of ReviewViewer? You probably just want copyWithZone:
Note: If you override copyWithZone to perform a shallow copy you are specifying this behavoir everywhere the object is copied.
为什么不能在多个地方使用类的同一个实例,而不是两个独立但相同的对象?这样就只有一个对象需要修改。一旦你制作了一份副本(深的或浅的),你就有了两个独立的对象——对一个对象的更改不会传播到另一个对象。
Instead of two separate but identical objects, why can't you just use the same instance of your class in more than one place? That way there's only ever one object to modify. Once you make a copy (deep or shallow), you have two independent objects--changes to one don't propagate to the other.