0

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

2
  • 1
    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. Commented Dec 22, 2012 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. Commented Dec 23, 2012 at 3:08

4 Answers 4

5

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];
Sign up to request clarification or add additional context in comments.

6 Comments

looks easiest if it were function local and the dict doesnt change.. but I think it will, ramy's way is nice
Hold up, @KennyTM, then how does VFL work? Didn't apple define NSDictionaryOfVariableBindings() specifically for this?
@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.
@PeterHosey You mean to tell me you've never heard of the Visual Format Language?
@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.
|
1

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;

Then:

NSString* key= @"objectName1"; // objectName1 or whatever
id object= [self valueForKey: key];
[object method];

5 Comments

-1 this isnt even reflection he wants/needs ... your solution works though so +1 = 0
He wants to get @"object" from object and viceversa.Isn't that reflection? With preprocessor it's unidirectional.
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
oh and there is almost as much reflection in obC as in java
Thanks Guys, I've got it working now with: id object= [self valueForKey: key]; [object method]; The dictionary-idea from KennyTM was also good, but the above one suited my application better. So thanks everyone for your ideas/solutions/help and especially Ramy for the "winning" solution!
-1

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

2 Comments

he wants to assign names to INSTANCES not classes
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
-1

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];

2 Comments

he wants instances by name -- if I understood him :D I think ramy & kenny are right
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/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.