iOS 6.0中的CADisplayLink不保留目标

我有这样的代码:

NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(updateFrame)]]; [invocation setTarget:self]; [invocation setSelector:@selector(updateFrame)]; displayLink_ = [[CADisplayLink displayLinkWithTarget:invocation selector:@selector(invoke)] retain]; [displayLink_ setFrameInterval:1]; [displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 

在iOS 6.0(在5.1中这段代码可以正常工作)当这段代码调用我有两个变种:EXC_BAD_ACCESS或’调用无法识别的选择器’调用“’。 似乎displayLinkWithTarget:selector:方法不保留目标。 当我添加[invocation retain]行时,代码变得正常工作。 这是iOS 6.0的错误吗?

这是有用的相关信息,而不是答案。

而不是使用NSInvokation你可以使用弱代理,就像我在这个问题的实际答案中所做的那样。 这很简单,这是代码:

JAWeakProxy.h:

 #import  @interface JAWeakProxy : NSObject @property (weak, nonatomic) id target; + (JAWeakProxy*)weakProxyWithTarget:(id)target; @end 

JAWeakProxy.m:

 #import "JAWeakProxy.h" @implementation JAWeakProxy + (JAWeakProxy*)weakProxyWithTarget:(id)target { JAWeakProxy* newObj = [self new]; newObj.target = target; return newObj; } - (BOOL)respondsToSelector:(SEL)sel { return [_target respondsToSelector:sel] || [super respondsToSelector:sel]; } - (id)forwardingTargetForSelector:(SEL)sel { return _target; } @end 

注意:这是ARC代码,您需要在weakProxyWithTarget: autorelease weakProxyWithTarget:如果不使用ARC。

我有同样的问题。 我实际上想要一个弱引用,但由于它被记录为强大并且在其他版本的iOS中表现得那么我使用弱代理对象将选择器转发到我真正希望它去的地方。 为了确保保留代理对象,我必须找到一种方法来安全地将其保留在破坏的iOS版本上,而不会为非破坏版本过度保留它。 我提出了一个非常优雅的单行解决方案(解释它的四行评论之后的行):

 JAWeakProxy* weakSelf = [JAWeakProxy weakProxyWithTarget:self]; _displayLink = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(displayLinkUpdate:)]; // Due to a bug in iOS 6, CADisplayLink doesn't retain its target (which it should and is // documented to do) so we need to ensure a strong reference to the weak proxy object is // created somewhere. We do this by adding it as an associated object to the display link // which means that it gets retained for as long as the display link object is alive. objc_setAssociatedObject(_displayLink, @"Retain the target, bitch!", weakSelf, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 

记得#import 。 使用关联对象很有用,因为它在释放显示链接时被释放,而在OS的非破坏版本上它只是意味着对象被显示链接保留两次。