I’m working on an iOS app with Cocos2D and I’m running into a lot of situations where I want to do something with a slight delay so I use a line of code like this:
[self scheduleOnce:@selector(do_something) delay:10];
The stuff that happens in do_something is only one line of code though.
Is there a way for me to define the function right in that line where I schedule it?
When I used to program with jQuery this is similar in what I’m trying to achieve:
$("a").click(function() {
alert("Hello world!");
});
See how function() is defined right there? Is there a way to do this in Objective-C?
Also, is there a name for this? For future searches? Because I find this hard to explain.
You can use
dispatch_afterto execute a block after a certain amount of time.I would refer to it as a time dispatched block.
EDIT: How to dispatch it only once.
So in your case: