removefromsuperview是否会释放scrollview的对象? [英] does removefromsuperview releases the objects of scrollview?

查看:114
本文介绍了removefromsuperview是否会释放scrollview的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(UIView *subview in [scrollView subviews]) {
    NSLog(@"subviews Count=%d",[[scrollView subviews]count]);
    //[subview release];
    [subview removeFromSuperview];
}

上述方法中的

如果我使用 [subview removeFromSuperview ]; 它工作正常......但如果我使用
[subview release]; 它崩溃了......我想知道那个如果两者相同或它们之间有什么区别吗?

in the above method if i use [subview removeFromSuperview]; it works fine...but if i use [subview release];It crashes..i want to know that if both are same or is there any difference between them?

推荐答案

@MathieuK是正确的,但值得深入研究,因为它是ObjC中一个非常重要的概念。你不应该在一个你没有 -retain 的对象上显式或隐式地调用 -release (通过调用其中一个三个魔术词)。您不要调用 -release 来取消分配对象。你调用它来释放你放置对象的hold 。 scrollview是否保留其子视图不是您的业务(它确实保留其子视图,但它仍然不是您的业务)。是否 -removeFromSuperview 调用 -release 也不是您的业务。这是在scrollview及其子视图之间。重要的是,当您关心它们时保留对象并在停止关注它们时释放它们,并让系统的其余部分负责保留和释放所关心的内容。

@MathieuK is correct, but it's worth digging deeper into this, because it's a very important concept in ObjC. You should never call -release on an object you didn't -retain explicitly or implicitly (by calling one of the Three Magic Words). You don't call -release in order to deallocate an object. You call it to release the hold you have put on the object. Whether scrollview is retaining its subviews is not your business (it does retain its subviews, but its still not your business). Whether -removeFromSuperview calls -release is also not your business. That's betweeen the scrollview and its subviews. All that matters is that you retain objects when you care about them and release them when you stop caring about them, and let the rest of the system take care of retaining and releasing what it cares about.

这篇关于removefromsuperview是否会释放scrollview的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆