Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyway to get string from variable name?

Say I have my class

@interface Person : NSObject { NSString *name; }

I need to get the name of NSString's within my class

Person *person = [[Person alloc] init];
NSLog(@"Name of variable %s\n", _NameofVariable_(person->name));

Thanks for the answers, here's the solution I came up from the replies

//returns nil if property is not found
-(NSString *)propertyName:(id)property {  
    unsigned int numIvars = 0;
    NSString *key=nil;
    Ivar * ivars = class_copyIvarList([self class], &numIvars);
    for(int i = 0; i < numIvars; i++) {
        Ivar thisIvar = ivars[i];
        if ((object_getIvar(self, thisIvar) == property)) {
            key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
            break;
        }
    } 
    free(ivars);
    return key;
}  
like image 615
hacksignal Avatar asked Mar 20 '10 21:03

hacksignal


People also ask

How do I find the string name of a variable?

To get a variable's name as a string: Use the globals() function to get a dictionary that implements the current module namespace. Iterate over the dictionary to get the matching variable's name. Access the list item at index 0 to get the name of the variable.

How do I turn a variable into a string?

We can convert numbers to strings through using the str() method. We'll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value. The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

How do you get a string variable in Python?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .

How do you read a variable name in Python?

We can access the variable names in python using the globals() function and the locals() function. The globals() function, when executed, returns a dictionary that contains all the variable names as string literals and their corresponding values.


3 Answers

As easy as

#define VariableName(arg) (@""#arg)

Then you do:

NSObject *obj;
NSString *str = VariableName(obj);
NSLog(@"STR %@", str);//obj
like image 96
Andrey Chernukha Avatar answered Oct 28 '22 02:10

Andrey Chernukha


You can get the names of a class's instance variables with the Objective-C runtime API function class_copyIvarList. However, this is rather involved, rarely done and almost never the best way to accomplish something. If you have a more specific goal in mind than mere curiosity, it might be a good idea to ask about how to accomplish it in Objective-C.

Also, incidentally, person.name doesn't specify an instance variable in Objective-C — it's a property call. The instance variable would be person->name.

like image 39
Chuck Avatar answered Oct 28 '22 01:10

Chuck


You might use preprocessor stringification and a bit of string twiddling:

NSUInteger lastIndexAfter(NSUInteger start, NSString *sub, NSString *str) {
    NSRange found = [str rangeOfString:sub options:NSBackwardsSearch];
    if(found.location != NSNotFound) {
        NSUInteger newStart = NSMaxRange(found);
        if(newStart > start)
            return newStart;
    }
    return start;
}

NSString *lastMember(NSString *fullName) {
    if(!fullName) return nil;

    NSUInteger start = 0;
    start = lastIndexAfter(start, @".", fullName);
    start = lastIndexAfter(start, @"->", fullName);

    return [fullName substringFromIndex: start];
}

#define NSStringify(v) (@#v)
#define _NameofVariable_(v) lastMember(NSStringify(v))
like image 2
Chris Johnsen Avatar answered Oct 28 '22 02:10

Chris Johnsen