Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've just found this question and wasn't happy with any of the answers. In my opinion neither make good use of the tools available, and passing around arbitrary information in arrays and dictionaries generally worries me.</p> <p>So, I went and wrote a small <code>NSObject</code> category that will invoke an arbitrary selector with a variable number of arguments:</p> <p><strong>Category Header</strong></p> <pre><code>@interface NSObject (NxAdditions) -(void)performSelectorInBackground:(SEL)selector withObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION; @end </code></pre> <p><strong>Category Implementation</strong></p> <pre><code>@implementation NSObject (NxAdditions) -(void)performSelectorInBackground:(SEL)selector withObjects:(id)object, ... { NSMethodSignature *signature = [self methodSignatureForSelector:selector]; // Setup the invocation NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; invocation.target = self; invocation.selector = selector; // Associate the arguments va_list objects; va_start(objects, object); unsigned int objectCounter = 2; for (id obj = object; obj != nil; obj = va_arg(objects, id)) { [invocation setArgument:&amp;obj atIndex:objectCounter++]; } va_end(objects); // Make sure to invoke on a background queue NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invocation]; NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init]; [backgroundQueue addOperation:operation]; } @end </code></pre> <p><strong>Usage</strong></p> <pre><code>-(void)backgroundMethodWithAString:(NSString *)someString array:(NSArray *)array andDictionary:(NSDictionary *)dict { NSLog(@"String: %@", someString); NSLog(@"Array: %@", array); NSLog(@"Dict: %@", dict); } -(void)someOtherMethod { NSString *str = @"Hello world"; NSArray *arr = @[@(1337), @(42)]; NSDictionary *dict = @{@"site" : @"Stack Overflow", @"url" : [NSURL URLWithString:@"http://stackoverflow.com"]}; [self performSelectorInBackground:@selector(backgroundMethodWithAString:array:andDictionary:) withObjects:str, arr, dict, nil]; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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