问题描述
for(UIView *subview in [scrollView subviews]) {NSLog(@"subviews Count=%d",[[scrollView subviews]count]);//[子视图发布];[子视图 removeFromSuperview];}在上述方法中,如果我使用 [subview removeFromSuperview]; 它工作正常...但如果我使用[subview release];它崩溃了..我想知道两者是否相同或它们之间有什么区别?
@MathieuK 是正确的,但值得深入挖掘,因为它是 ObjC 中一个非常重要的概念.你不应该在一个你没有显式或隐式 -retain 的对象上调用 -release(通过调用 三个神奇的词).您不会调用 -release 来释放对象.你调用它来释放 你 对对象的保持.scrollview 是否保留其子视图不是您的事(它确实保留了其子视图,但仍然不是您的事).-removeFromSuperview 是否调用 -release 也不是你的事.那是在滚动视图和它的子视图之间.重要的是,当你关心对象时你保留它们,当你不再关心它们时释放它们,让系统的其余部分负责保留和释放它所关心的.p>
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?
@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 的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)