释放已分配 UIView 的 NSMutableArray 也会释放 UIView 吗?
好的,这就是我正在做的事情。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您
复制
、分配
、保留
或新
某些内容,您有责任发送它release
或autorelease
。您说过
[[UIView alloc] init...]
,因此您必须释放
生成的对象。If you
copy
,alloc
,retain
, ornew
something, you are responsible for sending it eitherrelease
orautorelease
.You said
[[UIView alloc] init...]
so you mustrelease
the resulting object.自创建视图以来,您有责任释放它们。过程如下:
您创建保留计数为 1 的视图。
当它们被添加到数组中时,它将保留它们(保留计数 = 2)。
当您释放数组时,它将释放视图(保留计数 = 1)。
你仍然需要释放它们。
正确的代码是:
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: