释放已分配 UIView 的 NSMutableArray 也会释放 UIView 吗?

发布于 2024-11-05 23:59:03 字数 374 浏览 0 评论 0原文

好的,这就是我正在做的事情。

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];

[array release];

数组的释放会同时释放两个分配的UIView吗?

Ok so here's what I'm doing.

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];

[array release];

Will the releasing of the array, release the two allocated UIViews as well?

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

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

发布评论

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

评论(2

狼性发作 2024-11-12 23:59:03

如果您复制分配保留某些内容,您有责任发送它releaseautorelease

说过[[UIView alloc] init...],因此必须释放生成的对象。

If you copy, alloc, retain, or new something, you are responsible for sending it either release or autorelease.

You said [[UIView alloc] init...] so you must release the resulting object.

匿名的好友 2024-11-12 23:59:03

自创建视图以来,您有责任释放它们。过程如下:

您创建保留计数为 1 的视图。
当它们被添加到数组中时,它将保留它们(保留计数 = 2)。
当您释放数组时,它将释放视图(保留计数 = 1)。
你仍然需要释放它们。

正确的代码是:

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];
[tempview release];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];
[tempview2 release];

[array release];

You are responsible for releasing the views since you created them. Here is how it goes:

You create the views with a retain count of 1.
When they are added to the array, it will retain them (retain count = 2).
When you release the array, it will release the views (retain count = 1).
You still need to release them.

The correct code would be:

NSMutableArray *array = [[NSMutableArray alloc] init];  

UIView *tempview = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView];
[tempview release];

UIView *tempview2 = [[UIView alloc] initWithFrame:CGRectMake(15, 30, 320, 460)];
[array addObject:tempView2];
[tempview2 release];

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