LoginSignup
11
12

More than 5 years have passed since last update.

Objective-C の弱参照コンテナ

Posted at

NSHashTable, NSMapTable は追加されたオブジェクトの参照カウントを上げず、オブジェクトの参照カウントが 0 になった際にコンテナ要素を自動的に削除するモードがある。

NSHashTable

NSMutableSet のように集合を扱うコンテナ。
弱参照コンテナとして使用したい場合は次のように生成する。

NSHashTable* items = [NSHashTable weakObjectsHashTable];

NSMapTable

NSMutableDictionary のように連想配列を扱うコンテナ。
キーと値のどちらを弱参照にするかで 3 つの生成方法がある。

  • +(id)strongToWeakObjectsMapTable : 値を弱参照するコンテナを生成。
  • +(id)weakToStrongObjectsMapTable : キーを弱参照するコンテナを生成。
  • +(id)weakToWeakObjectsMapTable : キーと値の両方を弱参照するコンテナを生成。
11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12