objective c - How to pass a SEL to dispatch_async method -
i trying create generic method takes sel
parameter , passes dispatch_async
execution, clueless how execute passed in sel. can here me please.
// test.m -(void) executeme { nslog(@"hello"); } - (void)viewdidload { [super viewdidload]; sel executesel = @selector(executeme); [_pinst common_dispatch: executesel]; } // common.m -(void) common_dispatch:(sel) aselector { dispatch_async(idispatchworkerqueue, ^(void) { // how execute aselector here? }); }
you need have "target" parameter on common_dispatch
method since need call selector on specific object.
- (void)viewdidload { [super viewdidload]; sel executesel = @selector(executeme); [_pinst common_dispatch:executesel target:self]; } - (void)common_dispatch:(sel)aselector target:(id)target { dispatch_async(idispatchworkerqueue, ^(void) { [target performselector:aselector]; }); }
btw - standard naming conventions state method names should begin lowercase , use camelcase. method should commondispatch.
Comments
Post a Comment