As a general rule, we adopted a policy of avoiding NSInvocation except when absolutely necessary because the code tends to be hard to read, fragile, and a headache when refactoring (and a source of bugs).
Also, by avoiding NSInvocation and using straightforward call sites, the compiler is fully able to validate the code.
You can use the "dispatch_after" pattern:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self animationCoinInitWith_x: ...];
});
Or, if you want to stick with -performSelector:withObject:afterDelay: (which I find more readable):
@interface Foo : NSObject
@end
@implementation Foo
- (void)invokeBlock:(dispatch_block_t)aBlock
{
aBlock();
}
- (void)doIt:(int)x toIt:(int)y
{
NSLog(@"%s %d %d", __PRETTY_FUNCTION__, x, y);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Foo* foo = [Foo new];
dispatch_block_t block = ^{
[foo doIt:10 toIt:10];
};
[foo performSelector:@selector(invokeBlock:) withObject:[block copy] afterDelay:5];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
The above assumes ARC (hence, the lack of releases).