初心者向けにObjective-Cでのメモリ管理関連で私が持っている知識をまとめておく。
1) なるべくARC(Automatic Reference Counting)を使う。
2) IBOutlet にはARCの場合はweak、Non-ARCの場合はstrongを使う。
3) Non-ARCの場合、なるべくretain,releaseを書かない。
クラスのプライベート変数を使う代わりに、プライベートでstrongにしたプロパティを使い、dealloc時にnilを設定する。
@interface Coffee () { NSString* _bean; } @end @implementation - (void)dealloc { [_bean release]; [super dealloc]; } @end
つまり、上記のように書かずに、以下のように書く。
@interface Coffee () @property (strong) NSString* bean; @end @implementation - (void)dealloc { self.bean = nil; [super dealloc]; }
4) 静的アナライザの指摘を全てつぶす。