0

I have several objects that I want to use, like obj11, obj21, obj33, etc.

The choice of the correct object to use depends on some parameters defined by the user, like for example: size to use and kind of object.

so, the first number on the object name will represent the size and the second letter the kind. If the user choose the object size 2 and the object kind 1, the object to load will be "obj21".

Is there a way to reference an object based on its name as a NSString?

something like

NSString *name = [NSString stringWithFormat:@"obj%d%d", size, kind];
["use object name" doSomething];

I need something like NSSelectorFromString, but in this case, it runs a method based on its name. What I need is something like "NSObjectFromString", that references an object based on its name as string.

How do I do that? Thanks.

2 Answers 2

3

Since this is not possible, it would be a better idea to store your objects in an NSDictionary (or its mutable counterpart) and use the string you build as key for that object.

Here's an weird example:

NSMutableDictionary *objectDict = [[NSMutableDictionary alloc] init];

... init your objects somehow ...

// build your keys somehow and add the object to the dict
int size = 1;
int kind = 2;

NSString *name = [NSString stringWithFormat:@"obj%d%d", size, kind];
[objectDict addObject:obj12 forKey:name];

... Do some stuff ...

// access your object and call a method
id obj =  [objectDict objectForKey:name];
[obj doSomething];
Sign up to request clarification or add additional context in comments.

6 Comments

It's technically possible using Obj-C's runtime, but it would be an overkill solution compared with a NSDictionary.
Really? Cool, do you have any web reference for that? I'm curious.
@yinkou & Rubberduck, you can google for "Objective-C runtime tutorial" and "Objective-C associated objects". I have to say that ObjC's runtime use requires a deep understanding of the language. Some links: developer.apple.com/library/mac/#documentation/Cocoa/Reference/…, mikeash.com/pyblog/…, stackoverflow.com/questions/7819092/…, phrack.org/issues.html?issue=66&id=4
@atxe - this is too exoteric for my knowledge right now... can you come with a code answer? thanks.
why are you not satisfied with the NSDictionary method?
|
0

You can make a factory method and use NSSelectorFromString to call it dynamically.

More details here:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html

EDIT:

My answer to what I think is the same question:

id object = [[NSClassFromString(@"NameofClass") alloc] init];

Create objective-c class instance by name?

2 Comments

He wants an existing object, not to create a new one from a string.
Oh, I get it, a dynamic variable name!

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.