ios – NSTimer类别阻止实现替换选择器

ios – NSTimer类别阻止实现替换选择器,第1张

概述我对块和 objective-c很新,我试图用两者写我的第一个类别.我的想法是在NSTimer上创建一个类别,它将接收一个块作为参数,这个块将用于选择器调用.现在我有这个. // NSTimer+Additions.h#import <Foundation/Foundation.h>typedef void (^VoidBlock)();@interface NSTimer (NSTi 我对块和 objective-c很新,我试图用两者写我的第一个类别.我的想法是在NSTimer上创建一个类别,它将接收一个块作为参数,这个块将用于选择器调用.现在我有这个.

// NSTimer+Additions.h#import <Foundation/Foundation.h>typedef voID (^VoIDBlock)();@interface NSTimer (NSTimer_Additions)+ (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOol)repeats actions:(VoIDBlock)actions;@end#import "NSTimer+Additions.h"static VoIDBlock _voIDBlock;@interface NSTimer (Additionsprivate) // Private stuff- (voID)theBlock;@end@implementation NSTimer (NSTimer_Additions)+ (NSTimer *)scheduleTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOol)repeats actions:(VoIDBlock)actions {    [_voIDBlock release];    _voIDBlock = [actions copy];    NSTimer* timer = [[NSTimer alloc] initWithFireDate:[NSDate date]                                           interval:theSeconds                                            target:self                                           selector:@selector(theBlock)                                           userInfo:nil                                            repeats:repeats];    [timer fire];    return [timer autorelease];}- (voID)theBlock {    _voIDBlock();}@end

代码要点:https://gist.github.com/1065235

一切都编译好但我有以下错误:

2011-07-05 14:35:47.068 TesteTimer [37716:903] *由于未捕获的异常’NSinvalidargumentexception’终止应用程序,原因:'[NSTimer theBlock]:无法识别的选择器发送到类0x7fff70bb0a18′

我该如何使这个类别工作?

解决方法 除了错误的目标之外,你的主要缺陷是你使用静态变量.除了一个计时器,你将无法支持.

使用block作为被调用方法的参数.

@interface NSTimer (Additionsprivate) // Private stuff- (voID)theBlock:(VoIDBlock)voIDBlock;@end@implementation NSTimer (NSTimer_Additions)+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOol)repeats actions:(VoIDBlock)actions {    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:@selector(theBlock:)]];    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:theSeconds                                                   invocation:invocation                                                      repeats:repeats];    [invocation setTarget:timer];    [invocation setSelector:@selector(theBlock:)];    Block_copy(actions);    [invocation setArgument:&actions atIndex:2];    Block_release(actions);    return timer;}- (voID)theBlock:(VoIDBlock)voIDBlock {    voIDBlock();}@end

使用关联引用的问题是泄漏,因为释放块没有好处.

早期的方法使用关联参考

您可以使用associative references将块附加到该特定NSTimer实例.

@implementation NSTimer (NSTimer_Additions)+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)theSeconds repeats:(BOol)repeats actions:(VoIDBlock)actions {    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:@selector(theBlock)]];    NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:theSeconds                                                   invocation:invocation                                                      repeats:repeats];    [invocation setTarget:timer];    [invocation setSelector:@selector(theBlock)];    objc_setAssociatedobject(timer,@"Block",actions,OBJC_ASSOCIATION_copY);    return timer;}- (voID)theBlock {    VoIDBlock _voIDBlock = (VoIDBlock)objc_getAssociatedobject(self,@"Block");    _voIDBlock();}@end
总结

以上是内存溢出为你收集整理的ios – NSTimer类别阻止实现替换选择器全部内容,希望文章能够帮你解决ios – NSTimer类别阻止实现替换选择器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1016730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存