tags:

views:

41

answers:

2

I'm trying to read property name but I'm not shore how. Is it possible in ObjC to get string "theProperty" from self.theProperty?

I know how to read all properties (with "class_copyPropertyList") and their names (with "property_getName") but couldn't find a way to do something like:

NSString *text = [self.theProperty somehowReadThePropertyName]; 
// expected result is: text == @"theProperty"

Any ideas?

A: 

A problem you have is that when you call self.theProperty, you are getting an object of whatever type that property is. That object isn't generally going to know that it is part of another object with a particular property name.

You'll need to get the property name from a higher level than just calling it.

Abizern
+1  A: 

Here's how you would get a string representation of the getter for your property:

NSString *selectorName = NSStringFromSelector(@selector(theProperty));

And since you've already used property_getName to return a C string, you can get an NSString like this:

NSString *propertyName = [NSString stringWithCString:property_getName(property)
                                            encoding:NSUTF8StringEncoding];

But, what are you trying to accomplish? There may be a far better way to do what you need to do.

kubi
Thanks. Code NSString *selectorName = NSStringFromSelector(@selector(theProperty)); is working. BUT other version doesn't return NSString and XCode complains with "passing argument 1 of 'property_getName' from incompatible pointer type".
Uroš Milivojević
Whatever you're passing into the `property_getName` function is the wrong type.
kubi