Ja, Sie können NSPointerFunctionsWeakMemory verwenden. Facebook KVOController verwendet auch NSHashTable mit dieser Option, 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 {
// silence deprecated warnings
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
_infos = [infos initWithOptions:NSPointerFunctionsZeroingWeakMemory|NSPointerFunctionsObjectPointerPersonality capacity:0];
#pragma clang diagnostic pop
}
#endif
_lock = OS_SPINLOCK_INIT;
}
return self;
}
auch sehen, für bequemere Art und Weise, können Sie weakObjectsHashTable
Gibt eine neue Hash-Tabelle zum Speichern schwachen Verweises auf seinen Inhalt.
Rückgabewert Eine neue Hash-Tabelle, die die Optionen NSHashTableZeroingWeakMemory und NSPointerFunctionsObjectPersonality und hat eine Anfangskapazität von 0.
Das Dokument verwendet, ist ein wenig alt, aber es ist wahr. Siehe NSHipster NSHashTable & NSMapTable
NSHashTableZeroingWeakMemory: This option has been deprecated. Instead use the NSHashTableWeakMemory option
Beachten Sie auch, dass
NSHashTableWeakMemory Gleich NSPointerFunctionsWeakMemory
sollten Sie beachten, dass NSHashTable in OSX nur verfügbar ist, und nicht iOS. –
@SandyChapman 'NSHashTable' ist seit iOS auf iOS verfügbar. Überprüfen Sie die Kopfzeile 'NSHashTable.h'. – TylerTheCompiler
@DrBeardface Interessant, dass [die Dokumente] (https://developer.apple.com/library/mac/documentation/cocoa/reference/NSHashTable_class/Introduction/Introduction.html) nicht sagt, dass es dann unterstützt wird. ** BEARBEITEN: ** Sieht aus, als würde ich die falschen Dokumente ansehen. [This one] (https://developer.apple.com/library/ios/documentation/cocoa/reference/NSHashTable_class/Introduction/Introduction.html) sagt, dass es unterstützt wird. –