Thursday, 15 May 2014

ios - NSHashTable with weak refrences -



ios - NSHashTable with weak refrences -

i want to utilize nshashtable keeping weak references contained objects. regarding other customizable behaviors(including equality check), want exact same behavior nsset(so practically want have nsset weak references). can give me illustration on how initialize such hash table?

would next suffice: [nshashtable hashtablewithoptions:nspointerfunctionsweakmemory]

also nshashtable weak references automatically remove de-allocated objects?

thanks.

yes, can utilize nspointerfunctionsweakmemory. facebook kvocontroller utilize nshashtable option, see kvocontroller

- (instancetype)init { self = [super init]; if (nil != self) { nshashtable *infos = [nshashtable alloc]; #ifdef __iphone_os_version_min_required _infos = [infos initwithoptions:nspointerfunctionsweakmemory|nspointerfunctionsobjectpointerpersonality capacity:0]; #elif defined(__mac_os_x_version_min_required) if ([nshashtable respondstoselector:@selector(weakobjectshashtable)]) { _infos = [infos initwithoptions:nspointerfunctionsweakmemory|nspointerfunctionsobjectpointerpersonality capacity:0]; } else { // quiet deprecated warnings #pragma clang diagnostic force #pragma clang diagnostic ignored "-wdeprecated-declarations" _infos = [infos initwithoptions:nspointerfunctionszeroingweakmemory|nspointerfunctionsobjectpointerpersonality capacity:0]; #pragma clang diagnostic pop } #endif _lock = os_spinlock_init; } homecoming self; }

also, more convenient way, can utilize weakobjectshashtable

returns new hash table storing weak references contents.

return value new hash table uses options nshashtablezeroingweakmemory , nspointerfunctionsobjectpersonality , has initial capacity of 0.

the document little bit old, true. see nshipster nshash​table & nsmap​table

nshashtablezeroingweakmemory: alternative has been deprecated. instead utilize nshashtableweakmemory alternative

note that

nshashtableweakmemory equal nspointerfunctionsweakmemory

ios objective-c cocoa ios7 ios6

No comments:

Post a Comment