2011年9月6日 星期二

Xcode 學習筆記(三)iphone Application Developer(2009~2010 winter) 之二



因為這堂課要講解的東西比較多 所以把它分成多次筆記來寫囉!


==================================================
Time = 15:08
Object Lifecycle




Time = 23:08
Reference Counting
• Every object has a retain count
這邊提到 每個object 都有一個 retain 值
初始值為0


■ Defined on NSObject
■ As long as retain count is > 0, object is alive and valid
• alloc and copy create objects with retain count == 1
使用 alloccopy 方法去建立objects 會使得 retain 值為 1


• retain increments retain count
retain 會增加 1
• release decrements retain count
release 則會減少 1
• When retain count reaches 0, object is destroyed
當 retain 值為0 的時候 , object 就會毀滅


• dealloc method invoked automatically
dealloc 這個方法就會自動被呼叫
■ One-way street, once you’re in -dealloc there’s no turning back
這是單行道 當呼叫 dealloc 後,記憶體就會被釋放


這個投影片就可以清楚看到 
Retain 透過 alloc , retain 增加1
release 則是減少1 
當Retain 為0時,dealloc 會自動被呼叫收回記憶體(釋放記憶體)。


所以關於Object Lifecycle 總整理如下

Object Lifecycle Recap
Objects begin with a retain count of 1
Increase and decrease with -retain and -release
When retain count reaches 0, object deallocated automatically
You never call dealloc explicitly in your code 
      ■ Exception is calling -[super dealloc]
      ■ You only deal with alloc, copy, retain, release


沒有留言: