特性属性“retain” 似乎不起作用?

发布于 2024-07-30 01:48:43 字数 722 浏览 3 评论 0原文

我已经从众多 Apple 代码示例之一中实现了一些代码,但遇到了一些麻烦,因为其中一个属性的保留属性似乎不起作用。 这是属性声明:

@property (nonatomic, retain) EditingViewController *editingViewController;

这是代码:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        editingViewController = aController;
        [aController release];
    }
    return editingViewController;
}

我理解 (retain) 应该会导致赋值时保留计数增加 1; 但是,除非我自己发送[aController keep],或者发送[aController release],否则代码会失败。 我在这里缺少什么?

I've implemented a bit of code from one of the many Apple code examples, but I'm having a bit of trouble, because the retain attribute of one of the properties doesn't appear to be working. Here's the property declaration:

@property (nonatomic, retain) EditingViewController *editingViewController;

And here's the code:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        editingViewController = aController;
        [aController release];
    }
    return editingViewController;
}

I understand that (retain) is supposed to cause the retain count to increase by 1 on assignment; however, the code fails unless I do send [aController retain] myself, or don't send [aController release]. What am I missing here?

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

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

发布评论

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

评论(2

负佳期 2024-08-06 01:48:43

当您引用editingViewController时,它相当于self->editingViewController,即对ivar的访问。

如果您想使用 getter 或 setter,则需要使用 self.editingViewController 或等效的 [self setEditingViewController:aController]

这就是为什么我更喜欢使用与属性名称不同的 ivar,例如:

EditingViewController* i_editingViewController;

@property (nonatomic, retain) EditingViewController *editingViewController;

@synthesize editingViewController = i_editingViewController;

然后你可以将惰性 getter 编写为:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        i_editingViewController = [[EditingViewController alloc] init];
    }
    return i_editingViewController;
}

或者

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        self.editingViewController = aController;
        [aController release];
    }
    return i_editingViewController;
}

我可能会使用前一种方法(不调用 setter),因为 的值editViewController (任何观察者都可以看到)并没有真正改变,但无论哪种方式都应该可以正常工作,并且不同的名称(对于 ivar 和属性)有助于避免混淆或意外误用。 这也是对使用该属性的温和鼓励(因为它避免了稍微难看的前缀)。

请注意,Apple 保留 _ 前缀,并且 setter 和 getter 不应在 init/dealloc 例程中使用。

When you reference editingViewController, it is equivalent to self->editingViewController, i.e. an access to an ivar.

If you want to use a getter or setter, you need to use self.editingViewController, or equivalently [self setEditingViewController:aController].

This is why I prefer to use an ivar with a different name to the property, for example:

EditingViewController* i_editingViewController;

@property (nonatomic, retain) EditingViewController *editingViewController;

@synthesize editingViewController = i_editingViewController;

Then you can write your lazy getter as:

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        i_editingViewController = [[EditingViewController alloc] init];
    }
    return i_editingViewController;
}

or

- (EditingViewController *)editingViewController {
    // Instantiate the editing view controller if necessary.
    if (i_editingViewController == nil) {
        EditingViewController *aController = [[EditingViewController alloc] init];
        self.editingViewController = aController;
        [aController release];
    }
    return i_editingViewController;
}

I would probably use the former method (not invoking the setter) because the value of editingViewController (as seen by any observer) has not really changed, but either way should work fine and the different name (for ivar and property) help avoid the confusion or accidental misused. It is also a mild encouragement to use the property (since it avoids the slightly ugly prefix).

Note that Apple reserves the _ prefix, and that setters and getters should not be used in the init/dealloc routines.

千年*琉璃梦 2024-08-06 01:48:43

您必须编写 self.editingViewController 才能使用该属性。 只是“editingViewController”是对 Class 成员变量的直接访问,而 self.editingViewController 相当于 [self setEditingViewController:...] 并将执行适当的保留/释放工作。

You have to write self.editingViewController in order to use the property. Just "editingViewController" is a direct access to the Class member variable, whereas self.editingViewController is equivalent to [self setEditingViewController:...] and will do the appropriate retain/release job.

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