問題描述
所以我有一個支持 iOS 4 的項目,所以我所有的 IBOutlets 都是 __unsafe_unretained
甚至 IBOutlets 在 nib 中但在控制器主視圖之外(同一個 nib 中的單獨視圖)并且所有工作很棒.
So I had a project that supported iOS 4, so all my IBOutlets were __unsafe_unretained
even IBOutlets that were in the nib but outside the controllers main view (Separate View in the same nib) and all worked great.
時間到了,現(xiàn)在客戶只想支持 iOS 5,所以我們的團隊將所有 __unsafe_unretained
IBOutlets 更改為 __weak IBOutlets
但現(xiàn)在不支持的 IBOutlets主視圖內部設置為 nil
(viewdidload
除外),因此我們以后無法添加它們.
So the time has come and now the client wants to support only iOS 5 so our team changed all the __unsafe_unretained
IBOutlets for __weak IBOutlets
but now the IBOutlets that are not inside the main view are set to nil
(except in viewdidload
) so we are unable to add them later.
如果我考慮一下,這是有道理的,因為如果沒有視圖(主視圖)保留那些 IBOutlets,它們應該被釋放并歸零(我不知道這是否是正確的詞),所以解決方案是刪除來自那些 IBOutlets 的 __weak
If I think about it, it makes sense because if no view (main view) is retaining those IBOutlets they should be deallocated and zeroed (I don't know if that is the correct word), so the solution is to remove the __weak
from those IBOutlets
但是對我來說沒有意義的是為什么 unsafe_unretained
和 weak
之間的不同行為,在我的腦海中 unsafe_unretained
應該被釋放,當應用程序嘗試訪問它們時,它們應該指向一個無效的引用,然后應用程序應該崩潰.
But what doesn't make sense to me is Why the different behavior between unsafe_unretained
and weak
, in my head the unsafe_unretained
ones should be deallocated and when the app tries to access them, they should point to an invalid reference and then the app should crash.
我認為 unsafe__unretained 與 weak 相同,但沒有歸零.
I thought that unsafe__unretained was the same as weak but without the zeroing.
我錯過了什么嗎?
謝謝.
推薦答案
我認為 unsafe__unretained 與 weak 相同,但沒有歸零.
I thought that unsafe__unretained was the same as weak but without the zeroing.
是的,是的.
當 Cocoa 加載 nib 時,它會創(chuàng)建所有自動釋放的對象,因此在調用 viewDidLoad
時它們仍然存在.但是,自動釋放池的生命周期在控制權返回到運行循環(huán)時結束.此時,所有不屬于任何東西的對象都將消失,因此任何弱出口都將在此時歸零.
When Cocoa loads the nib, it creates all the objects autoreleased, so they are still there when viewDidLoad
is invoked. However, the autorelease pool has a lifetime that ends when control returns to the run loop. At this point all the objects that aren't owned by anything will go away so any weak outlets will be zeroed at that point.
對于大多數商店來說,這不是問題,因為 NIB 中的對象通常已經由某物擁有.因此,例如,視圖中的按鈕歸其父視圖所有.因此,擁有指向該按鈕的強大出口是矯枉過正或更糟的可能導致保留周期.
For most outlets, this is not a problem because objects in the NIB are already generally owned by something anyway. So, for instance, a button in a view is owned by its parent view. Having strong outlets that point to that button are therefore overkill or worse might result in a retain cycle.
頂級對象顯然沒有父視圖來擁有它們,因此它們需要由其他東西擁有,例如控制器或文件的所有者".如果你發(fā)現(xiàn)東西消失了,你需要在 File 的所有者中為它創(chuàng)建一個強大的 IBOutlet.
Top level objects obviously don't have a parent view to own them so they need to be owned by something else e.g. a controller or "File's Owner". If you are finding stuff disappears, you need to create a strong IBOutlet for it in File's owner.
有關更多詳細信息,請參閱 Apple 的文檔.
For more detail, see Apple's docs.
這篇關于weak 和 unsafe_unretained 的區(qū)別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!