1

I read a NSString from file and then I want to #define this as a UIColor so I can quickly change color's.

I want something to work like so:

#define GRAY [UIColor darkGrayColor]

Then read from file the string : @"GRAY" and put it in a NSString variable called kColor

And then paste this onto a backgroundcolor property like so:

myController.view.backgroundColor = kColor;

now this means I get :

myController.view.backgroundColor = @"GRAY";

I need:

myController.view.backgroundColor = GRAY;

for the #define to work. So how do i remove @"" from the string or typecast the string into a variable name? I'm not stuck on #define so if there is another way to get this to work, I'm open to that.

Help is greatly appreciated.

user715510
  • 13
  • 2

4 Answers4

2

You could load the colors into a NSDictionary.

// Put this in some global scope
NSDictionary *colors = [NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor darkGrayColor], @"GRAY", nil];

// using the dictionary
myController.view.backgroundColor = [colors objectForKey:kColor];
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • That works! And is so beautiful in it's simplicity that I don't know why I haven't thought of it myself ;) Thanks for helping me get unstuck. – user715510 Apr 19 '11 at 16:29
2

When you say 'read from file the string : @"GRAY"' do you mean at application run time? if so, then you cannot use #define(s), which are happening at compile time.

NWCoder
  • 5,296
  • 1
  • 26
  • 23
0

You can't do that. Objective-C is a compiled language, so you can't use a NSString as if it were a piece of code. You will need a way to return a proper color from a string/NSString read from a file.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Include LLVM libraries, compile yit and dynamically link :) –  Apr 19 '11 at 16:02
  • 1
    You can actually. You can turn strings into selectors or classes with `NSSelectorFromString()` and `NSClassFromString()`. – JeremyP Apr 19 '11 at 16:05
  • @JeremyP he's not asking about selector, but about a string that resolves to a define. – NWCoder Apr 19 '11 at 16:06
  • 1
    @NWCoder: I was referring to using an `NSString` as a piece of code in general. – JeremyP Apr 19 '11 at 16:16
  • @JeremyP: true. you can use a string as a selector of to instantiate a new class. But you can't use it as a generic piece of code that you would run with something like Javascript's `eval`. (which is apparently what OP wants to accomplish. although his/her question is not **that** clear) – Pablo Santa Cruz Apr 19 '11 at 16:25
0

Daniel's answer is probably the best. Since he hasn't provided any code, this is one implementation:

-(UIColor*) colorFromString: (NSString*) colorName
{
    static NSDictionary colors = nil;
    if (colors == nil)
    {
        colors = [NSDictionary dictionaryWithObjectsAndKeys: 
                                   [UIColor darkGreyColor], @"GRAY",
                                   [UIColor blueColor], @"BLUE",
                                   // etc
                                   nil];
        [colors retain];
    }
    return [colors objectForKey: colorName];
}
JeremyP
  • 84,577
  • 15
  • 123
  • 161