getting access through #defined values through strings?

Discussion in 'iPhone/iPad Programming' started by dantastic, Mar 1, 2011.

  1. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    Um, I may have painted myself into a corner here.

    I have a bunch of #define kMyValue @"Some Arbitrary String"
    I use these as it makes it handy to use @"kMyValue" as keys for storing a bunch of stuff in the core data db.

    So at one stage I end up with a dictionary with strings & keys. the keys are each named after my #define - example
    Code:
    #define kName @"Enter full name here"
    ...
    [myDict setObject:@"Dan Tastic" forKey:@"kName"];
    ...
    nameLabel.text = [myDict valueForKey:@"kName"];
    descriptionLabel.text = kName;
    
    Now I want to print out the *friendly* version of the key - grab the name from the #defined value - how do I do that?? how do I make a variable out of my string?? How do I programatically accomplish the above from an array of dictionary keys?
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    The code you show doesn't make sense. It should be this

    Code:
    [myDict setObject:@"Dan Tastic" forKey:kName];
    To print it out do this

    Code:
    NSLog(@"The value of kName is \"%@\"", kName);
     
  3. thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Right, and if I have a string as
    Code:
    NSString *myString = @"kName";
    
    Then I want to do
    Code:
    NSLog(@"The value of kName is %@", kName);
    
    But using the string in 'myString', something like
    Code:
    NSLog(@"The value of kName is %@", [myString value]);
    
     
  4. thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #4
    I should probably mention what I'm doing before this gets messy.

    I have a bunch of dictionaries, I need a handy key that I can use for everything, the dictionary itself and the core data model.

    When it comes to showing the user the friendly version of the key I figured it would be a good idea to just #define the friendly strings against the keys - they are in my array already.

    The simple way out is to just create another dictionary but I like this approach if it's possible:)
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    You basically cannot do what you are trying to do. #defines are replaced by the pre-processor. Basically it does a search and and replaced so every instance of kName will become @"Enter full name here". Then the compiler runs on that search/replaced code.
     
  6. thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #6
    Right. So another dictionary it is so I suppose..

    cheers!
     

Share This Page