Leem.fin Leem.fin - 5 months ago 19
Objective-C Question

directly perform a selector on a object

Say I have a selector:

(SEL)aSelector


and, I have an object
MyObj
.

The
aSelector
represents a function defined in the class of
MyObj
.

How can I perform the selector on
MyObj
?

(I found in NSObject class, there is a function:

- performSelector:onThread:withObject:waitUntilDone:

But it doesn't fit what I need, since the
withObject:
part there represents a parameter pass to the selector function, but what I need is to send selector message to this
myObj
)

===== Why I ask & What I want to achieve ===

I want to have a general function which accepts a Selector type parameter and a object on which the selector is supposed to perform. In this general function body, I use dispatch group to perform this selector on the object and wait for the selector function complete, my code snippet:

-(void)myGeneralFuncWithObject:(NSObject*)myObj andSelector:(SEL)aSelector{
...
dispatch_group_async(group, queue, ^{
// My question is how to write code to
// indicate performing aSelector on myObj here?
});

dispatch_group_notify(group, queue, ^{
// selector function finished here, I suppose.
});
}


CLARIFICATION:

I'd like to perform the selector on
myObj
, like I mentioned above. The class of
myObj
is also a subclass of
NSThread
, and I would like the selector to perform on this thread. So, in total, I want the selector to be performed on
myObj
and on
myObj
thread. That's all about it.

Answer

You can try this In MyObj.h

-(void)mySelWithOneArgument:(MyObj*)argument;

In MyObj.m

-(void)mySelWithOneArgument:(MyObj*)argument{
     //Do whateve
}

Now in your general function do this

-(void)myGeneralFuncWithObject:(NSObject*)myObj andSelector:   
(SEL)aSelector{
 ...
 dispatch_group_async(group, queue, ^{
      [myObj performSelector:aSelector withObject:instance];
});

dispatch_group_notify(group, queue, ^{
 // selector function finished here, I suppose.
});
}

And aSelector = @selector(mySelWithOneArgument:);//Dont forget the : at the end

Comments