xcode removefromsuperview 释放scrollview 的对象吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1340754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:45:40  来源:igfitidea点击:

does removefromsuperview releases the objects of scrollview?

objective-ciphonexcodememory-managementuiscrollview

提问by Rahul Vyas

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

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?

在上面的方法中,如果我使用 [subview removeFromSuperview];它工作正常......但是如果我使用[subview release];它崩溃......我 想知道两者是否相同或者它们之间有什么区别?

回答by Rob Napier

@MathieuK is correct, but it's worth digging deeper into this, because it's a very important concept in ObjC. You should never call -releaseon an object you didn't -retainexplicitly or implicitly (by calling one of the Three Magic Words). You don't call -releasein order to deallocate an object. You call it to release the hold youhave 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 -removeFromSuperviewcalls -releaseis 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 itcares about.

@MathieuK 是正确的,但值得深入研究这一点,因为它是 ObjC 中一个非常重要的概念。你永远不应该调用-release一个你没有-retain显式或隐式调用的对象(通过调用三个魔法词之一)。您不会-release为了释放对象而调用。你调用它来释放对对象的保持。scrollview 是否保留其子视图与您无关(它确实保留其子视图,但仍然不是您的事)。是否-removeFromSuperview来电-release也不是你的事。这是在滚动视图和它的子视图之间。重要的是,当你关心对象时你保留它们,当你停止关心它们时释放它们,让系统的其余部分负责保留和释放关心的东西。

回答by NSSec

The retain count of your subviews is probably 1. When you call [subview release];the retain count becomes 0 and the subview is released from memory. The subsequent access to subview (to call removeFromSuperview) crashes because subview isn't there anymore.

您的子视图的保留计数可能是 1。当您调用时[subview release];,保留计数变为 0,并且子视图从内存中释放。随后对子视图(调用removeFromSuperview)的访问崩溃,因为子视图不再存在。

In this case you should just call [subview removeFromSuperview]because removeFromSuperviewwill call releaseon subview itself.

在这种情况下,您应该只调用[subview removeFromSuperview]因为removeFromSuperview将调用release子视图本身。

回答by dreamlax

You need to revise the Cocoa Memory Management.

您需要修改Cocoa 内存管理

You simply do not release things that you have not explicitly allocated or retained yourself.

你只是不释放你自己没有明确分配或保留的东西。

回答by M.Othman

No they are not the same , if you checked the

不,它们不一样,如果你检查了

retainCount

保留计数

after

removeFromSuperView

从超级视图中移除

you will see that nothing is changed , so you can add the same view again without allocating it!!

你会看到什么都没有改变,所以你可以再次添加相同的视图而无需分配它!!

plus you should never release an object that you have never allocated!!

加上你永远不应该释放一个你从未分配过的对象!!