1

Possible Duplicate:
object with the name of a string

I want to call the code below, but be able to change "itemInstance" with a stringe variable. Is this possible?

UIImage *image = [itemInstance getImage:itemNumber];

EDIT: I just read about the NSSelectorFromString method which appears to be what I need to use, but I have no idea how it's supposed to implemented. Any ideas?

3
  • What are you really trying to accomplish?
    – Caleb
    Jun 4, 2012 at 16:30
  • possible duplicate of object with the name of a string, Object name from string
    – jscs
    Jun 4, 2012 at 17:18
  • @Caleb, I said what I was trying to accomplish in my first sentence... Phillip answered the question. Josh, yes, those do appear to be similar. I didn't see those before. However, I think that the question is still helpful because it is slightly different. Jun 4, 2012 at 18:54

2 Answers 2

3

Not exactly. The simplest way to get that effect is to build a NSDictionary with your strings as the keys and the matching instances as the values. Then it becomes something like:

UIImage *image = [[dict objectForKey:instanceName] getImage:itemNumber];
2
  • Could I use an NSArray instead? I'm going to try that now... Jun 4, 2012 at 18:04
  • If you can identify the instance in an array, I guess. I picked a dictionary because you said you wanted to select by string. Jun 4, 2012 at 21:44
0

I solved it by selecting the instance from an NSArray. I am selecting Phillips answer though because it got me to where I needed to get.

Not the answer you're looking for? Browse other questions tagged or ask your own question.