Getting the Key or Key Path of an object

Discussion in 'Mac Programming' started by BadWolf13, Feb 3, 2011.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    Ok, so KVC is great, it allows us to use strings in place of the object or instance variable's name. Question though, can it work the other way around? Is there a method that will return the object or variable's name as an NSString object? For example, I can use KVC to replace;

    [book.title];

    with

    [book valueForKey:mad:"title"];

    Now, on the other hand, say I want to pass the name of an object as a NSString. In this case, the object's name being "book."

    I hope I'm making sense here. Is it possible?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I guess i would say that such a path is only relevant in relation to another object. If book was the favoriteBook member of some other object there would be a path to book and its properties from that object. Otherwise, who are you passing valueForKey: to?

    Maybe you mean something else, like having code in an NSString you want to execute, but that wasn't my first interpretation.

    -Lee
     
  3. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #3
    Like I said, I wasn't sure if I was explaining myself well. It's not the valueForKey that I'm looking for, that's easy. What I'm looking for is the key, as an NSString.

    Let me put it this way, KVC allows us to call an instance variable, or an instance object using a string. I would like to get the name of an instance variable or object as a string, so that I can use it in an error message. Does this make more sense?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Do you want the name of the variable pointing to the object, or the name of the class the object is an instance of?

    As for instance variables:
    NSStringFromSelector
    http://developer.apple.com/library/....html#//apple_ref/c/func/NSStringFromSelector
    to use this you'd need a selector... Presumably for the accessor for the ivar. You can get that with NSSelectorFromString:... but at that point you already have the string...

    Not sure if this is helping... I'm having trouble thinking of when you don't know the name of the variable you're accessing, etc.

    -Lee
     
  5. BadWolf13, Feb 3, 2011
    Last edited: Feb 3, 2011

    BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #5
    It's not that I don't know, it's that I want a re-usable way to pass that variable, or pointer name into a string. For instance, instead of typing;

    Code:
    if ([myString isEqualToString:@""]
         NSLog(@"myString is blank.");
    
    Could I type;

    Code:
    if ([myString isEqualToString:@""]
         NSLog(@"%@ is blank", [myString [I]aMethod[/I]]);
    
    Where [myString aMethod] would return the value @"myString"?
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    The fundamental problem is that myString is a label in your code for a small bit of memory that holds an address. That address will be to an object elsewhere in memory. There could be 238,839 variables all with unique names all pointing to the same object. myObject is not the name of the object, and after compilation that name doesn't exist (other than debugging symbols, but not in the code itself). Even if it did, it really doesn't have anything to do with the object. myObject could point to many different objects while a program is running, and any object could have any number of pointer variables out there pointing to it. The object itself is not really tied to the name of any variable.

    Unfortunately I don't think there is any way to do this. By the time a message gets to an object, which pointer variable used to send it is not knowable unless &myObject was passed in, and then that's only the address. You could have a registerVariable:(me **) withName: (NSString *), then store a table in your object, then make a getNameForVariableAddress:( me **), but it would be a lot of legwork for nothing, IMO.

    -Lee
     

Share This Page