具有在 getter 中返回对象副本并在 setter 中保留对象的属性

发布于 2024-10-11 02:30:05 字数 377 浏览 6 评论 0原文

我在理解属性时遇到问题。 如果我希望属性不被其访问器修改(通过复制返回)并通过引用设置(保留),我应该编写什么样的属性。

例子 :

MyType* theAttribute = MyObject.attribute;
[theAttribute changeSomething]; // Does not have to change the MyObject attribute, working with a copy return

MyType tmpObject = [[MyType alloc] init];
MyObject.attribute = tmpObject; // Want a retain here
[tmpObject release];

I have a problem understanding properties.
What kind of property should I write if I want an attribute not be modified by its accessors (return by copy) and be set by reference (retain).

Example :

MyType* theAttribute = MyObject.attribute;
[theAttribute changeSomething]; // Does not have to change the MyObject attribute, working with a copy return

MyType tmpObject = [[MyType alloc] init];
MyObject.attribute = tmpObject; // Want a retain here
[tmpObject release];

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

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

发布评论

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

评论(4

森林很绿却致人迷途 2024-10-18 02:30:05

在标头中将属性声明为保留:

@property (nonatomic. retain) MyType *attribute;

在实现文件中合成 iVar:

@synthesize attribute;

但提供您自己的 getter,它将覆盖合成的 getter:

- (MyType)attribute {
    return [attribute copy];
}

我知道我不会自动释放返回的对象,但由于问题没有指定 iOS,我是从垃圾收集环境的角度写这篇文章的。

declare the property as retain in the header:

@property (nonatomic. retain) MyType *attribute;

In the implementation file synthesize the iVar:

@synthesize attribute;

But provide your own getter which will override the synthesized one:

- (MyType)attribute {
    return [attribute copy];
}

I know I'm not autoreleasing the returned object, but since the question doesn't specify iOS, I'm writing this from the perspective of a garbage collected environment.

北恋 2024-10-18 02:30:05

在这种情况下,您应该自己编写 getter 和 setter。

In such case you should write getter and setter by yourself.

岛徒 2024-10-18 02:30:05

您可能会编写自己的访问器,但具体如何取决于您是否希望复制的“get”返回自动释放:

  1. 完成“复制”的惯用方法是拥有一个正常的“为访问器保留”属性,并支持 -copy-mutableCopy 方法。后两者是语义复制对象的标准,并且返回的对象应该具有保留所有权。

  2. 如果您想让访问器 -foo 返回“foo”的副本,以便调用者无法操作内部版本,那没问题,但是您需要自己将其写入看起来像这样:

-

- (Foo *)foo
{
    return [[foo copy] autorelease];
}

You'd probably write your own accessor(s), but exactly how depends on whether you want the copied "get" return to be autoreleased or not:

  1. The idiomatic way of accomplishing "copy" this is to have a normal "retain" property for accessors, and support the -copy or -mutableCopy method also. The latter two are the standard for semantically copying an object, and the returned object should have a retain ownership.

  2. If you want to have the accessor -foo return a copy of "foo" so that the caller can't manipulate the internal version, that's OK, but then you'd write it yourself to look like this:

-

- (Foo *)foo
{
    return [[foo copy] autorelease];
}
又怨 2024-10-18 02:30:05

我建议不要通过副本返回。这会阻碍你进行内存管理。是自动释放的吗?不保留?问题太多了。

相反,我推荐这个:

MyType* attr = [MyType typeWithMyType:MyObject.attribute];

或者其他什么。

I would suggest not returning by copy. This will snag you up in memory management. Is it auto-released? Not retain'd? Too many questions.

Instead, I recommend this:

MyType* attr = [MyType typeWithMyType:MyObject.attribute];

or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文