問題描述
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模板網!