Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C va_list and selectors
    primarykey
    data
    text
    <p>Is it possible to use <code>@selector</code> and <code>performSelector:</code> (or similar) with methods using variable arguments list?</p> <p>I'm writing a class that can be assigned a delegate to override the default behavior. In the presence of a delegate select method calls made on an instance of that class will be forward to the same corresponding delegate method, some which use variable argument lists.</p> <p>So, for instance, I need to be able to create retrieve <code>SEL</code> reference and message the delegate object with a method such as this:</p> <pre><code>- (void)logEventWithFormat:(NSString *)format, ... { va_list argList; id del = self.delegate; if (del != nil &amp;&amp; [del conformsToProtocol:@protocol(AProtocolWithOptionalMethods)] &amp;&amp; [del respondsToSelector:@selector(logEventWithFormat:)]) { // Perform selector on object 'del' with 'argList' } } </code></pre> <p>I am assuming this is not possible, hence the similar method declaration in the Foundation framework - in <code>NSString</code>:</p> <pre><code>- (id)initWithFormat:(NSString*)format, ...; </code></pre> <p>and </p> <pre><code>- (id)initWithFormat:(NSString *)format arguments:(va_list)argList; </code></pre> <p>I assume that the protocol I wish to delegate to should suggest the implementation of:</p> <pre><code>- (void)logEventWithFormat:(NSString *)format arguments:(va_list)argList; </code></pre> <p>so I the selector <code>@selector(logEventWithFormat:arguments:)</code> can be used an called with:</p> <pre><code>[del performSelector:@selector(logEventWithFormat:arguments:) withObject:format withObject:argList]; </code></pre> <p>I just wondered if I was missing something or going the long way around to achieve what I'm trying to?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload