supportsWeakPointers
,这些方法记录在ARC的发行说明中,但在实际的运行时和框架中从未提及过.还可以观察到,这个 method is in fact ignored in practice.的另一个方法是allowWeakReference,它没有在任何地方记录,但在NSObject.h中声明如下. - (BOol)allowsWeakReference NS_UNAVAILABLE;- (BOol)retainWeakReference NS_UNAVAILABLE;
尝试在运行时调用allowWeakReference会导致程序崩溃,并显示以下堆栈跟踪
objc[17337]: Do not call -_isDeallocating.#0 0x00007fff9900f768 in _objc_trap ()#1 0x00007fff9900f8aa in _objc_fatal ()#2 0x00007fff9901bd90 in _objc_rootIsDeallocating ()#3 0x00007fff97e92ce9 in -[NSObject _isDeallocating] ()#4 0x00007fff97b5fad5 in -[NSObject(NSObject) allowsWeakReference] ()#5 0x00007fff97dfe021 in -[NSObject performSelector:] ()......#11 0x00007fff97a5fd32 in __-[NSNotificationCenter addobserver:selector:name:object:]_block_invoke_1 ()#12 0x00007fff97dafaaa in _CFXNotificationPost ()#13 0x00007fff97a4bfe7 in -[NSNotificationCenter postNotificationname:object:userInfo:] ()#14 0x00007fff8fa0460f in -[NSApplication _postDIDFinishNotification] ()#15 0x00007fff8fa04375 in -[NSApplication _sendFinishLaunchingNotification] ()#16 0x00007fff8fa0303c in -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] ()#17 0x00007fff8fa02d9d in -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] ()#18 0x00007fff97df9591 in -[NSObject performSelector:withObject:withObject:] ()#19 0x00007fff97a827eb in __-[NSAppleEventManager setEventHandler:andSelector:forEventClass:andEventID:]_block_invoke_1 ()#20 0x00007fff97a81772 in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] ()#21 0x00007fff97a81600 in _NSAppleEventManagerGenericHandler ()#22 0x00007fff96623c25 in aedispatchAppleEvent ()#23 0x00007fff96623b03 in dispatchEventAndSendReply ()#24 0x00007fff966239f7 in aeProcessAppleEvent ()#25 0x00007fff92101af9 in AEProcessAppleEvent ()#26 0x00007fff8fa001a9 in _DPSNextEvent ()#27 0x00007fff8f9ff861 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()#28 0x00007fff8f9fc19d in -[NSApplication run] ()#29 0x00007fff8fc7ab88 in NSApplicationMain ()
那么,如果两种方法都不能使用,那么如何测试一个对象是否支持形成对它的弱引用呢?
解决方法 结束了黑客攻击.适合我需要的东西.@implementation NSObject (MAWeakReference)static NSSet *weakRefUnavailableClasses = nil;+ (voID)load { // https://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.HTML weakRefUnavailableClasses = [NSSet setWithObjects: // Classes that don't support zeroing-weak references @"NSATSTypesetter",@"NScolorSpace",@"NSFont",@"NSFontManager",@"NSFontPanel",@"NSImage",@"NSMenuVIEw",@"NSParagraphStyle",@"NSSimpleHorizontalTypesetter",@"NStableCellVIEw",@"NSTextVIEw",@"NSVIEwController",@"NSWindow",@"NSWindowController",// In addition @"NSHashtable",@"NSMaptable",@"NSPointerarray",// Todo: need to add all the classes in AV Foundation nil];}- (BOol)ma_supportsWeakPointers { if ([self respondsToSelector:@selector(supportsWeakPointers)]) return [[self performSelector:@selector(supportsWeakPointers)] boolValue]; // NOTE: Also test for overrIDen implementation of allowsWeakReference in NSObject subclass. // We must use a bit of Hackery here because by default NSObject's allowsWeakReference causes // assertion failure and program crash if it is not called by the runtime Method defaultMethod = class_getInstanceMethod([NSObject class],@selector(allowsWeakReference)); Method overrIDenMethod = class_getInstanceMethod([self class],@selector(allowsWeakReference)); if (overrIDenMethod != defaultMethod) return [[self performSelector:@selector(allowsWeakReference)] boolValue]; // Make sure we are not one of classes that do not support weak references according to docs for (Nsstring *classname in weakRefUnavailableClasses) if ([self isKindOfClass:NSClassFromString(classname)]) return NO; // Finally,all tests pass,by default objects support weak pointers return YES;}@end总结
以上是内存溢出为你收集整理的objective-c – 如何测试目标c对象是否可以在ARC下使用弱引用?全部内容,希望文章能够帮你解决objective-c – 如何测试目标c对象是否可以在ARC下使用弱引用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)