I have been using #selector today for the first time and have not been able to work out how to do the following? How would you write the #selector if you had more than one argument?
No arguments:
-(void)printText {
NSLog(#"Fish");
}
[self performSelector:#selector(printText) withObject:nil afterDelay:0.25];
Single argument:
-(void)printText:(NSString *)myText {
NSLog(#"Text = %#", myText);
}
[self performSelector:#selector(printText:) withObject:#"Cake" afterDelay:0.25];
Two arguments:
-(void)printText:(NSString *)myText andMore:(NSString *)extraText {
NSLog(#"Text = %# and %#", myText, extraText);
}
[self performSelector:#selector(printText:andMore:) withObject:#"Cake" withObject:#"Chips"];
Multiple Arguments: (i.e. more than 2)
NSInvocation
- (id)performSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject
From the Documentation:
This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.
so in your case you would use:
[self performSelector:#selector(printText:andMore:)
withObject:#"Cake"
withObject:#"More Cake"]
As an alternative for NSInvocation when you have more than two parameters, you can use NSObject's -methodForSelector: as in the following example:
SEL a_selector = ...
Type1 obj1 = ...
Type2 obj2 = ...
Type3 obj3 = ...
typedef void (*MethodType)(id, SEL, Type1, Type2, Type3);
MethodType methodToCall;
methodToCall = (MethodType)[target methodForSelector:a_selector];
methodToCall(target, a_selector, obj1, obj_of_type2, obj_of_type3);
I had an issue where I needed to use the afterDelay along with multiple arguments to my #selector method. Solution? Use a wrapper function!
Say this is the function I want to pass to #selector:
-(void)myFunct:(NSString *)arg1 andArg:(NSString *)arg2 andYetAnotherArg:(NSString *)arg3;
Obviously, I can't even use withObject: withObject: here, so, make a wrapper!
-(void)myFunctWrapper:(NSArray *)myArgs {
[self myFunct:[myArgs objectAtIndex:0] andArg:[myArgs objectAtIndex:1] andYetAnotherArg:[myArgs objectAtIndex:2]];
}
and use it by doing:
NSArray *argArray = [NSArray arrayWithObjects:string1,string2,string3,nil];
[self performSelector:#selector(myFunctWrapper:) withObject:argArray afterDelay:1.0];
This way I can have multiple arguments and use the selector with delay.
#selector(printText:andMore:)
[self performSelector:#selector(printText:andMore) withObject:#"Cake" withObject:#"More Cake"];
Another option is to use an even shorter syntax:
#import <objc/message.h> // objc_msgSend
...
((void (*)(id, SEL, Type1, Type2, Type3))objc_msgSend)(target, a_selector, obj1, obj_of_type2, obj_of_type3);
Elaborating on Ben-Uri's answer, which can be written way shorter.
E.g. calling the UIView method - (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view can
be done as follows:
SEL selector = #selector(covertPoint:toView:);
IMP method = [viewA methodForSelector:selector];
CGPoint pointInB = method(viewA, selector, pointInA, viewB);
As KennyTM pointed out, the selector syntax is
#selector(printText:andMore:)
You call it with
performSelector:withObject:withObject.
... if you need more arguments or different types, you need to use NSIvocation
Using NSInvocation as you specify you can create an NSObject category that implements
- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments;
Something like:
- (void)performSelector:(SEL)aSelector withObjects:(NSArray *)arguments
{
NSMethodSignature *signature = [self methodSignatureForSelector: aSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
[invocation setSelector: aSelector];
int index = 2; //
for (NSObject *argument in arguments) {
[invocation setArgument: &argument atIndex: index];
index ++;
}
[invocation invokeWithTarget: self];
}
from: iOS - How to implement a performSelector with multiple arguments and with afterDelay?
Related
I would like to pass a variable argument list from one method (functionOne) to another (functionTwo). Everything works fine, except that I have not been able to figure out how to setup the va_list in functionTwo in a way where I can access the first parameter in the va_list. Using va_arg advances to the second parameter in the va_list. Thx.
- (void)functionOne:(NSString *)configFiles, ... {
va_list args;
va_start(args, configFiles);
[self functionTwo:args];
va_end(args);
}
- (void)functionTwo:(va_list)files {
NSString *file;
while ((file = va_arg(configFiles, NSString *))) {
...
}
}
The first variadic argument is not the argument passed to va_start – it's the one immediately following it. If you want functionTwo: to have access to the configFiles string, you'll need to pass it in explicitly.
See Technical Q&A QA1405: Variable arguments in Objective-C methods.
Methods that take variable arguments are known as variadic methods.
Keep in mind that the implementation of an Objective-C method is just
a block of code, like a C function. The variadic argument macros
described in the stdarg(3) manual page work the same way in a method
as they do in an ordinary function.
Here's an example of an Objective-C category, containing a variadic
method that appends all the objects in a nil-terminated list of
arguments to an NSMutableArray instance:
#import <Cocoa/Cocoa.h>
#interface NSMutableArray (variadicMethodExample)
// This method takes a nil-terminated list of objects.
- (void)appendObjects:(id)firstObject, ...;
#end
#implementation NSMutableArray (variadicMethodExample)
- (void)appendObjects:(id)firstObject, ... {
id eachObject;
va_list argumentList;
if (firstObject) // The first argument isn't part of the varargs list,
{ // so we'll handle it separately.
[self addObject: firstObject];
// Start scanning for arguments after firstObject.
va_start(argumentList, firstObject);
while (eachObject = va_arg(argumentList, id)) // As many times as we can get an argument of type "id"
[self addObject: eachObject]; // that isn't nil, add it to self's contents.
va_end(argumentList);
}
}
#end
A solution that I use for debugging purposes is like
-(void) debug:(NSString*)format, ... {
if (level < MXMLogLevelDebug) return;
if(format == nil) return;
va_list args, args_copy;
va_start(args, format);
va_copy(args_copy, args);
va_end(args);
NSString *logString = [[NSString alloc] initWithFormat:format
arguments:args_copy];
NSString *funcCaller = #"";
NSArray *syms = [NSThread callStackSymbols];
if ([syms count] > 1) {
funcCaller = [syms objectAtIndex:1];
}
NSString *logMessage = [NSString stringWithFormat:#"%# DEBUG: %#", funcCaller, logString];
NSLog(#"%#",logMessage);
}
The side-effect that this can have is that you have to add a guard on the args to be sure is not NULL.
Hi im new to objective C and was wondering if someone might be able to help me with this. I have a few different methods each requiring 3 input values and normally call it using
[self methodA:1 height:10 speed:3]
but the method name I want to read from a string in a plist so for example if the string was methodB i would get
[self methodB:1 height:10 speed:3]
for "methodC"
[self methodC:1 height:10 speed:3]
and so on.
Any ideas how I might do this I tried defining the string as a Selector using NSSelectorFromString
NSString *string = [plistA objectForKey:#"method"];
SEL select = NSSelectorFromString(string);
[self performSelector:select:c height:b speed:a];
However this did not work either any help would be greatly appreciated.
Have tried the solution below but could not get to work here is what i've tried.
So just to recap I have methods such as
spawnEnemyA:2 withHeight:3 withSpeed:4
spawnEnemyB:3 withHeight:2 withSpeed:5
and I want to read the values I want to pass to these methods as well as the method type from a plist file. my code is as follows, //////////////////////////////////////////////////////////////
//These are the values I read from the plist that I want my method to use
int a = [[enemySettings objectForKey:#"speed"] intValue];
int b = [[enemySettings objectForKey:#"position"] intValue];
int c = [[enemySettings objectForKey:#"delay"] intValue];
// I Also read the method name from the plist and combine it into a single string
NSString *method = [enemySettings objectForKey:#"enemytype"];
NSString *label1 = #"spawn";
NSString *label2 = #":withHeight:withSpeed:";
NSString *combined = [NSString stringWithFormat:#"%#%#%#",label1, method,label2];
//Check that the string is correct get spawnEnemyA:withHeight:withSpeed:
CCLOG(#"%#",combined);
//This is the Invocation part
NSInvocation * invocation = [ NSInvocation new ];
[ invocation setSelector: NSSelectorFromString(combined)];
[ invocation setArgument: &c atIndex: 2 ];
[ invocation setArgument: &b atIndex: 3 ];
[ invocation setArgument: &a atIndex: 4 ];
[ invocation invokeWithTarget:self ];
[invocation release ];
////////////////////////////////////////////////////////////////////
The code compiles without any errors but the methods are not called. Any ideas? Cheers
You can't use performSelector for a method with 3 (or more) arguments.
But for your information, here's how to use it:
SEL m1;
SEL m2;
SEL m3;
m1 = NSSelectorFromString( #"someMethodWithoutArg" );
m2 = NSSelectorFromString( #"someMethodWithAnArg:" );
m1 = NSSelectorFromString( #"someMethodWithAnArg:andAnotherOne:" );
[ someObject performSelector: m1 ];
[ someObject performSelector: m2 withObject: anArg ];
[ someObject performSelector: m2 withObject: anArg withObject: anOtherArg ];
For methods with more than 2 arguments, you will have to use the NSInvocation class.
Take a look at the documentation to learn how to use it.
Basically:
NSInvocation * invocation = [ NSInvocation new ];
[ invocation setSelector: NSStringFromSelector( #"methodWithArg1:arg2:arg3:" ) ];
// Argument 1 is at index 2, as there is self and _cmd before
[ invocation setArgument: &arg1 atIndex: 2 ];
[ invocation setArgument: &arg2 atIndex: 3 ];
[ invocation setArgument: &arg3 atIndex: 4 ];
[ invocation invokeWithTarget: targetObject ];
// If you need to get the return value
[ invocation getReturnValue: &someVar ];
[ invocation release ];
In general, this kind of dynamism often indicates an anti-pattern. Colluding data with implementation in this fashion is not generally a best practice.
Sometimes, though, it is necessary. If you are going down this path, then given that your various method declarations likely look like:
- (void)methodAWidth:(NSUInteger)w height:(NSUInteger)h speed:(NSUInteger)s;
- (void)methodBWidth:(NSUInteger)w height:(NSUInteger)h speed:(NSUInteger)s;
- (void)methodCWidth:(NSUInteger)w height:(NSUInteger)h speed:(NSUInteger)s;
You would probably want something like:
NSString *selName = [NSString stringWithFormat:#"method%#Width:height:speed:", ... one of #"A", #"B", or #"C" ....];
SEL selector = NSelectorFromString(selName);
Then:
if (![target respondsToSelector:selector])
return; // no can do
void (*castMsgSend)(id, SEL, NSUInteger, NSUInteger, NSUInteger) = (void*)objc_msgSend;
castMsgSend(target, selector, 1, 10, 3);
Every method call is compiled down to a call to objc_msgSend(). By doing the above, you are creating a fully type-safe/type-checked call site that goes through the normal Objective-C messaging mechanism, but the selector is dynamically defined on the fly.\
While performSelector: (and multi-arg variants) are handy, they can't deal with non-object types.
And, as MacMade pointed out in a comment, watch out for floating point and structure returns. They use different variants of objc_msgSend() that the compiler automatically handles in the normal [foo bar] case.
You can directly use objc_msgsend:
NSString *methodName = [plistA objectForKey:#"method"];
objc_msgSend(self, methodName, c, b, a);
Mind that the selector must include all pieces, eg #"method:height:speed:"
You should replace the following line with:
[self performSelector:select:c height:b speed:a];
and write following:
[self performSelector:select withObject:[NSArray arrayWithObjects:c,b,a,nil]];
I need your help. I have some problems with NSInvocation 'getReturnValue:' method. I want to create UIButton programmatically, and even more, I want to create it dynamically using NSInvocation and through passing value through the NSArray (that's why I wrapped UIButtonTypeRoundedRect).
Listing.
NSLog(#"Button 4 pushed\n");//this code executed when button pushed
Class cls = NSClassFromString(#"UIButton");//if exists {define class},else cls=nil
SEL msel = #selector(buttonWithType:);
//id pushButton5 = [cls performSelector:msel withObject:UIButtonTypeRoundedRect];//this code works correctly,but I want to do this by NSInvocation
//---------------------------
NSMethodSignature *msignatureTMP;
NSInvocation *anInvocationTMP;
msignatureTMP = [cls methodSignatureForSelector:msel];
anInvocationTMP = [NSInvocation invocationWithMethodSignature:msignatureTMP];
[anInvocationTMP setTarget:cls];
[anInvocationTMP setSelector:msel];
UIButtonType uibt_ = UIButtonTypeRoundedRect;
NSNumber *uibt = [NSNumber numberWithUnsignedInt:uibt_];
NSArray *paramsTMP;
paramsTMP= [NSArray arrayWithObjects:uibt,nil];
id currentValTMP = [paramsTMP objectAtIndex:0];//getParam from NSArray
NSInteger i=2;
void* bufferTMP;
//if kind of NSValue unwrapp it.
if ([currentValTMP isKindOfClass:[NSValue class]]) {
NSUInteger bufferSize = 0;
NSGetSizeAndAlignment([currentValTMP objCType], &bufferSize, NULL);
bufferTMP = malloc(bufferSize);
[currentValTMP getValue:bufferTMP];//copy currentVal to bufer
[anInvocationTMP setArgument:bufferTMP atIndex:i];// The +2 represents the (self) and (cmd) offsets
}else {
[anInvocationTMP setArgument:¤tValTMP atIndex:i];//Again,+2 represents (self) and (cmd) offsets
}
void* result = malloc([[cls methodSignatureForSelector:msel] methodReturnLength]);
[anInvocationTMP invoke];
[anInvocationTMP getReturnValue:result];
NSLog(#"sizeof(UIButton)=%i,sizeof(result)=%i,methodreturnlength = %i,sizeof(*result)=%i",class_getInstanceSize(NSClassFromString(#"UIButton")),sizeof(result),[[cls methodSignatureForSelector:msel] methodReturnLength],sizeof(*result));
id pushButton5;
pushButton5=result;
//---------------------------
NSLog output: sizeof(UIButton)=140,sizeof(result)=4,methodreturnlength = 4,sizeof(*result)=1
The problem is that value from NSInvocation is pointer of size 4 bytes. It should point to UIButton object,size of 140 bytes. But actually refers to 1 byte data. So what does happen with UIButton object,that should be initialized by 'buttonWithType:'?
Added after getting some answers:
To clarify: I want to get UIButton object, but after this code id pushButton5 = (id) result; ,when I try to work with pushButton5,it causes EXC_BAD_ACCESS. Can someone help me?
Can this happen because of this?
Class cls = NSClassFromString(#"UIButton");
...
[anInvocationTMP setTarget:cls];
It is correct, isn't it?
If you're using ARC, I would replace this:
void* result = malloc([[cls methodSignatureForSelector:msel] methodReturnLength]);
[anInvocationTMP invoke];
[anInvocationTMP getReturnValue:result];
With this:
CFTypeRef result;
[anInvocationTMP invoke];
[anInvocationTMP getReturnValue:&result];
if (result)
CFRetain(result);
UIButton *pushButton5 = (__bridge_transfer UIButton *)result;
The reason is that the invokation's return object is not retained, so it will go away, even if you immediately assign it to an object reference, unless you first retain it and then tell ARC to transfer ownership.
result has type void* and your sizeof(*result) expression is measuing sizeof(void), which apparently yields 1 in your compiler.
To check type of an Objective-C object, use isKindOfClass: method:
id resObj = (id)result;
if ([resObj isKindOfClass:[UIButton class]])
NSLog(#"mazel tov, it's a button");
Just be sure it's really an objective-c object first.
The return value is a UIButton* not a UIButton. Thus everything looks fine in your code.
It's not a problem.
First, getReturnValue: should come after the invocation. So,
[anInvocationTMP getReturnValue:result];
[anInvocationTMP invoke];
should be
[anInvocationTMP invoke];
[anInvocationTMP getReturnValue:result];
Next, you should forget about the size of UIButton itself. What buttonWithType returns is UIButton*, not UIButton. In Objective-C, you should never directly deal with the object itself. You should always work with a pointer to the object.
Finally, (Objective-)C's sizeof operator is purely compile-time operation. So, sizeof(*result) doesn't know at all what result points to at the run time. But it doesn't matter... you shouldn't care about the size of UIButton, as I already told you.
Really answer that I needed was... How do you think where ? Yes, in documentation.
Use the NSMethodSignature method methodReturnLength to determine the size needed for buffer :
NSUInteger length = [[myInvocation methodSignature] methodReturnLength];
buffer = (void *)malloc(length);
[invocation getReturnValue:buffer];
When the return value is an object, pass a pointer to the variable (or memory) into which the object should be placed :
id anObject;
NSArray *anArray;
[invocation1 getReturnValue:&anObject];
[invocation2 getReturnValue:&anArray];
So the problem solved. Thanks for your respond guys.
What is the easiest way to make an nsinvocation with target, selector and arguments?
This page adresses several nice constructors for nsinvocation. http://www.a-coding.com/2010/10/making-nsinvocations.html, but they doesen't cover arguments.
The following code created as a category will give you a nice way to create NSInvocations easily.
.h file
#interface NSInvocation (BetterInit)
+ (id)invocationWithSelector:(SEL)selector target:(id)target arguments:(id)firstArgument, ...;
#end
.m file
#implementation NSInvocation (BetterInit)
+ (id)invocationWithSelector:(SEL)selector target:(id)target arguments:(id<NSObject>)firstObject, ... {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[target methodSignatureForSelector:selector]];
[invocation setTarget:target];
[invocation setSelector:selector];
id eachObject;
va_list argumentList;
int index = 2; // should start at 2 since 0 and 1 is reserved _self and _cmd
// The first argument isn't part of the varargs list
if (firstObject) {
[invocation setArgument:&firstObject atIndex:index];
index ++;
// Start scanning for arguments after firstObject.
va_start(argumentList, firstObject);
// As many times as we can get an argument of type "id"
while(eachObject = va_arg(argumentList, id)) {
[invocation setArgument:&eachObject atIndex:index];
index ++; // Increase index
}
va_end(argumentList);
}
NSAssert(index == [[invocation methodSignature] numberOfArguments], #"you should have same number of arguments as methodsselector");
return invocation;
}
#end
This class will allow you to create an NSInvocation easily for any message, with any argument types:
http://cocoawithlove.com/2008/03/construct-nsinvocation-for-any-message.html
I have a class that contains a collection of objects. I am trying to create a method that will return the first member of the collection matching a supplied predicate.
Here is the collection method:
...
//predicate is a boolean method that accepts an object as its single parameter
-(id<Notation>) getFirstChildMatching: (SEL) predicate declaredInInstance:(id) instance
{
NSMethodSignature *sig = [[instance class] instanceMethodSignatureForSelector:predicate];
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:sig];
[myInvocation setTarget:instance];
[myInvocation setSelector:predicate];
int numItems = childNotations.count;
for(int i=0;i< numItems;i++)
{
id<Notation> thisNotation = [childNotations objectAtIndex:i];
[myInvocation setArgument:thisNotation atIndex:2];
BOOL result =NO;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue:&result];
if (result)
return thisNotation;
}
return nil;
}
I have created a test class that tests this method.
Here is the test method plus the predicate:
- (void) testGetFirstChildMatching
{
Leaf *line1 = [[Leaf alloc] initWithValue:1 step:Step_A andNumber:1];
Leaf *line2 = [[Leaf alloc] initWithValue:2 step:Step_B andNumber:2];
SEL mySelector = #selector(valueIs1:);
id<CompositeNotation> compositeNotation = [[CompositeNotation alloc] init];
[compositeNotation addNotation:line1];
[compositeNotation addNotation:line2];
id notation = [compositeNotation getFirstChildMatching: mySelector declaredInInstance:self];
STAssertEquals(YES, [notation isKindOfClass:[Leaf class]], #"Should be of type Leaf: %#", notation);
//Leaf *found = ((Leaf *)notation);
STAssertEquals([notation value], line1.value, #"Should have found line 1 with value 1: actual %i", [notation value]);
[line1 release];
[line2 release];
}
-(BOOL) valueIs1: (Leaf *) leaf
{
if (leaf.value == 1)
return YES;
return NO;
}
What I am finding is that on the "if (leaf.value == 1)" line I am getting an "unrecognized selector sent to class". What doesn't make sense is that the debugger can see the value property and it's value so the object clearly has that select.
Any ideas?
btw Leaf implements the notation protocol
Typo in function definition?
-(BOOL) valueIs1: (Leaf *) Leaf // <== should be "leaf" not "Leaf" ?
The fact that you're getting an "unrecognized selector sent to class", not instance, implies that leaf is a Class. Two things to check:
Ensure that Leaf's initializer initWithValue is returning an object.
Ensure that addNotation: is correctly adding Leafs to the array.
I eventually found the problem.
It was this line
[myInvocation setArgument:thisNotation atIndex:2];
it should have been
[myInvocation setArgument:&thisNotation atIndex:2];
Thanks