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;
}
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.
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.
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 .
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.
As easy as
#define VariableName(arg) (@""#arg)
Then you do:
NSObject *obj;
NSString *str = VariableName(obj);
NSLog(@"STR %@", str);//obj
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
.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With