1

I have a class where I need to create an Object from using reflection.

say I have the following class (got by class-dump or else):

@interface SomeClass : NSObject
{
    - (void)dealloc;
    - (id)initWithArguments:(char *)value1 arg2:(int)value2;
}

Now I want to create a new object from it using reflection:

Class theClass = [instance class];
SEL selector = NSSelectorFromString(@"initWithArguments:arg2:");
id newInstanceOfClass = [[theClass alloc] performSelector:selector withObject:????];

How can I pass the secondary parameter?

Note that I am not able to change the layout of SomeClass.

Eun
  • 4,146
  • 5
  • 30
  • 51

2 Answers2

1

You can use performSelector:withObject:withObject: method if you are going to pass 2 parameters. If you need to pass more than two the best way is create custom class which will hold all of your parameters and pass the class with performSelector:withObject: method. You cannot pass primitive variable so replace int with NSNumber.

//EXTENDED

If you want to pass more than 2 parameter and you cannot amend the method to accept custom object you can use NSInvocation:

// List of yours parameters
NSString *s1 = @"parameter 1";
NSString *s2 = @"parameter 2";
NSString *s3 = @"parameter 3";

// your selector
SEL selector = @selector(testParam1:param2:param3:);

if([self respondsToSelector:selector]) { // just to make sure there is right method to run,
// you have to change it to [theClass alloc] respond to selector instead of self
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
    [inv setSelector:selector];
    [inv setTarget:self]; // change it to the object you want to call the parameter (theClass)

    [inv setArgument:&s1 atIndex:2]; //arguments 0 and 1 are reserved for self and cmd you have to start passing your parameters with index 2
    [inv setArgument:&s2 atIndex:3];
    [inv setArgument:&s3 atIndex:4];
    [inv invoke];
}
Greg
  • 25,317
  • 6
  • 53
  • 62
  • and if there are more then 2 paremeters? – Eun Apr 25 '14 at 14:04
  • See my answer. If there is more that two you can create custom method and pass it as a parameter. I can show test code if you like. – Greg Apr 25 '14 at 14:05
  • The problem is that I am not able to change the class I want to call, so passing multiple parameters as one object does not work. – Eun Apr 25 '14 at 14:06
  • @MartinR I haven't seen the other answer because I was writing this one. This is working example so maybe Eun will use it. – Greg Apr 25 '14 at 14:37
  • @Greg: Then I apologize. I have deleted my comment. – Martin R Apr 25 '14 at 14:50
1

Create a header for the class and import it. Use the header to interact with the class (so you won't be making any performSelector... calls).

If for some reason you can't do that, use NSInvocation instead of performSelector....

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    You were faster :-) - Here are some NSInvocation examples: http://stackoverflow.com/questions/313400/nsinvocation-for-dummies. – Martin R Apr 25 '14 at 14:14
  • could you make an example for my case? – Eun Apr 25 '14 at 14:16
  • The question linked by @MartinR has a number of good examples that you can learn from. The process is the same for pointer variables and primitive variables. – Wain Apr 25 '14 at 14:19