Take the 2-minute tour ×

another easy one, for you guys I mean:

I need to call a method on an existing object like this:

[object method];

The name of the object however, is different (this code is for a superclass, the children all have different names) each time. So I want to call the object with an NSString like this:

NSString *objectName = @"object";
[objectName method];

I've read about NSClassFromString (I've been looking for hours now), but that doesn't work. Then I get the following error: "no known class method for selector 'method'".

Do you guys know what I'm doing wrong?

Thanks in advance, Frans

share|improve this question
This is not a natural thing to do in Objective-C, and is in fact kind of difficult. If you'd explain what you're actually trying to do (e.g. "I want to be able to send a message to one of two objects depending on a flag"), we could probably suggest something easier and more idiomatic. –  Chuck Dec 22 '12 at 21:24
NSClassFromString returns a class (as its name says), not the contents of a variable. A function that did return a variable would either have to know about all variables everywhere, effectively making all variables global and thus destroying any separation of knowledge, or be able to determine the scope of any caller (e.g., what method of what class the caller is in), and know what variables are visible to that scope. A daunting problem, whose solution is of dubious utility. –  Peter Hosey Dec 23 '12 at 3:08
add comment

4 Answers

up vote 1 down vote accepted

There isn't reflection in C, all you can do is to put the object in a dictionary or make it be an ivar.

For example:

@property (nonatomic,strong) id objectName1; // with ARC
@property (nonatomic,strong) id objectName2;
@property (nonatomic,strong) id objectNameN;


NSString* key= @"objectName1"; // objectName1 or whatever
id object= [self valueForKey: key];
[object method];
share|improve this answer
-1 this isnt even reflection he wants/needs ... your solution works though so +1 = 0 –  Daij-Djan Dec 22 '12 at 18:24
He wants to get @"object" from object and viceversa.Isn't that reflection? With preprocessor it's unidirectional. –  Ramy Al Zuhouri Dec 22 '12 at 18:28
he wants to get instances from a name.. variable names as in java arent known at compile time. class members yes but then he could use KVC –  Daij-Djan Dec 22 '12 at 19:00
oh and there is almost as much reflection in obC as in java –  Daij-Djan Dec 22 '12 at 19:02
You're right, I've just read it. –  Ramy Al Zuhouri Dec 22 '12 at 19:23
show 2 more comments

The name of a variable is only known at compile-time. There is no way to get a variable automatically from an NSString. You could, however, but an NSDictionary and call the method, e.g.:

NSDictionary* dict = @{@"object": object, @"obj2": obj2};
NSString* objectName;
[dict[objectName] method];
share|improve this answer
looks easiest if it were function local and the dict doesnt change.. but I think it will, ramy's way is nice –  Daij-Djan Dec 22 '12 at 18:26
Hold up, @KennyTM, then how does VFL work? Didn't apple define NSDictionaryOfVariableBindings() specifically for this? –  CodaFi Dec 22 '12 at 21:27
@CodaFi: What do you mean by “VFL”? If you mean valueForKey:, how it works is described in the documentation. You're right about the function—it does exactly what the first line of this answer does. –  Peter Hosey Dec 23 '12 at 3:11
@PeterHosey You mean to tell me you've never heard of the Visual Format Language? –  CodaFi Dec 23 '12 at 3:14
@CodaFi: Oh, that. Never heard it called “VFL” before. Anyway, you're right—you create a dictionary using that function and give it to the constraint when you create it. That's a different sense of “variable” from what KennyTM and the questioner are talking about—they're talking about variables in the C sense, not pairs in a dictionary. –  Peter Hosey Dec 23 '12 at 3:19
show 1 more comment

There is no such thing that you are searching for, but you can prevent this error by using isKindOfClass and then performing that method.

share|improve this answer
he wants to assign names to INSTANCES not classes –  Daij-Djan Dec 22 '12 at 18:25
I already mentioned that there is no such thing what he wants, but he can make use of conditions to call a particular method, that is the way I found can be done –  DivineDesert Dec 22 '12 at 18:29
add comment

If I have understood this correctly and you are talking about class methods, the problem is that you cannot "directly" send a message to the class you´re creating. You have to use performSelector.

SEL method = NSSelectorFromString(@"method");
Class class = NSClassFromString(@"SomeClass");
[(id)class performSelector:method];
share|improve this answer
he wants instances by name -- if I understood him :D I think ramy & kenny are right –  Daij-Djan Dec 22 '12 at 21:12
It'd be helpful to see this in context. There's probably a better way to solve it. Have you looked through the objc runtime ref? developer.apple.com/library/mac/#documentation/Cocoa/Reference/… –  estobbart Dec 22 '12 at 21:47
add comment

Your Answer


