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.

Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

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];
yinkou
  • 5,756
  • 2
  • 24
  • 40
  • It's technically possible using Obj-C's runtime, but it would be an overkill solution compared with a `NSDictionary`. – atxe Oct 15 '12 at 17:06
  • Really? Cool, do you have any web reference for that? I'm curious. – yinkou Oct 15 '12 at 17:21
  • 1
    @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: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html, http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html, http://stackoverflow.com/questions/7819092/how-can-i-add-properties-to-an-object-at-runtime, http://www.phrack.org/issues.html?issue=66&id=4 – atxe Oct 16 '12 at 00:14
  • @atxe - this is too exoteric for my knowledge right now... can you come with a code answer? thanks. – Duck Oct 16 '12 at 20:19
  • why are you not satisfied with the NSDictionary method? – yinkou Oct 16 '12 at 20:24
  • @atxe As i dig more and more in the runtime, i notice that this can't really work. It is possible to create, extend classes and objects or manipulate in really fascinating ways at runtime, (i love that, thanks!) but since the variable name of the pointer where the object is stored is gone after compiling, there is (since i can see it) no real solution even by using runtime functions. – yinkou Oct 16 '12 at 21:16
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?

Community
  • 1
  • 1
Chris McCall
  • 10,317
  • 8
  • 49
  • 80