3

I wish to write a macro, to be used within any method, which references the method's selector. I do not wish to pass the literal name of the method. For example:

#define RERUN [self performSelector:{something} withObject:nil afterDelay: 0.0]

where the "{something}" in the above would resolve to the selector of whatever method the macro was used in.

Is there some way to do this?

newacct
  • 119,665
  • 29
  • 163
  • 224
Michael57
  • 85
  • 7

2 Answers2

5

_cmd represents the selector of the current method -- it is a hidden argument (like self).

if you never need arguments, or nil is suitable for your purpose - all you need to do is write:

#define RERUN [self performSelector:_cmd]
justin
  • 104,054
  • 14
  • 179
  • 226
  • 3
    Your RERUN is subtly different from the original though — yours will occur right then on the current call stack, the original will schedule the call on the current run loop but not immediately call anything. – Tommy Dec 08 '11 at 13:56
  • @Tommy good point (+1) -- i thought the original was odd for that reason. the difference (for those who are wondering) is that the implementation i posted will be called immediately (it may also recurse infinitely if the condition is never met and cause a stack overflow). using the `afterDelay:` variant, `self` will be retained, and the message will be enqueued in the current run loop and happen in the future. – justin Dec 08 '11 at 14:20
  • Thanks, _cmd is exactly what I wanted. In my case, my purpose was specifically to give the run loop some cycles and then re-call the method. – Michael57 Dec 08 '11 at 16:23
  • You're welcome, and `afterDelay:` was exactly what you wanted in that case. – justin Dec 08 '11 at 16:42
1

Methods get an implicit argument _cmd, which is the selector.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48