2

I've looking for a way to get a property name as StringValue from inside a method.

Lets say:

My class has X Subviews from the Type UILabel.

@property (strong, nonatomic) UILabel *firstLabel;
@property (strong, nonatomic) UILabel *secondLabel;
[...]

and so on.

Inside the method foo, the views are iterated as followed:

-(void) foo 
{

for (UIView *view in self.subviews) {
 if( [view isKindOfClass:[UILabel class]] ) {
 /*
codeblock that gets the property name.
*/

 }
}
}

The Result should be something like that:

THE propertyName(NSString) OF view(UILabel) IS "firstLabel"

I've tried class_getInstanceVariable, object_getIvar and property_getName without Success.

For example, the code for:

[...]
property_getName((void*)&view)
[...]

Returns:

<UILabel: 0x6b768c0; frame = (65 375; 219 21); text = 'Something'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6b76930>>

But i'm looking for this kind of result: "firstLabel" , "secondLabel" and so on.


Solved

As in the Reply of graver described the solution is: class_copyIvarList which returns the name of the Ivars.

Ivar* ivars = class_copyIvarList(clazz, &count);
NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
    const char* ivarName = ivar_getName(ivars[i]);
    [ivarArray addObject:[NSString  stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
}
free(ivars);

See posts: https://stackoverflow.com/a/2302808/1228534 and Objective C Introspection/Reflection

Cœur
  • 37,241
  • 25
  • 195
  • 267
Joe Even
  • 21
  • 1
  • 3
  • 1
    see this post: http://stackoverflow.com/a/2302808/1228534 – graver Jun 06 '12 at 11:19
  • possible duplicate of [Objective C Introspection/Reflection](http://stackoverflow.com/questions/2299841/objective-c-introspection-reflection) – Joshua Nozzi Jun 06 '12 at 12:51
  • this is exactly what i'am looking for! class_copyIvarList did the job! many thanks!!! – Joe Even Jun 11 '12 at 11:47
  • From your description I still don't get how class_copyIvarList allows you to extract the property name that POINTS TO a given object, using only the object and 'self' to work with. You'll get a +1 from me if you answer your own question and point out the steps to solve it. – eric Mar 19 '13 at 18:30

2 Answers2

1

Easy way to get a specific property name without running a loop

Lets say custom object is like below

@interface StoreLocation : NSObject
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSNumber *lat;
@property (nonatomic, strong) NSNumber *lon;
@property (nonatomic, strong) NSString *street;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSString *code;
@end


@interface AppleStore : NSObject

@property (nonatomic, strong) StoreLocation *storeLocation;

@end

Below Objective macros will get the desired result

#define propertyKeyPath(property) (@""#property)
#define propertyKeyPathLastComponent(property) [[(@""#property)componentsSeparatedByString:@"."] lastObject]

Use the code below to get property name

NSLog(@"%@", propertyKeyPath(appleStore.storeLocation)); //appleStore.storeLocation
NSLog(@"%@", propertyKeyPath(appleStore.storeLocation.street)); //appleStore.storeLocation.street
NSLog(@"%@", propertyKeyPathLastComponent(appleStore.storeLocation)); //storeLocation
NSLog(@"%@", propertyKeyPathLastComponent(appleStore.storeLocation.street)); //street

Source : http://www.g8production.com/post/78429904103/get-property-name-as-string-without-using-the-runtime

user3894518
  • 677
  • 5
  • 10
  • that link is crap. this is just doing string manipulation. If you just do propertyKeyPath(buzz.lightyear) it'd work – SonicBison May 07 '15 at 22:52
0

untested code from Getting an array of properties for an object in Objective-C

id currentClass = [self class];
NSString *propertyName;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++) {
    objc_property_t property = properties[i];
    propertyName = [NSString stringWithCString:property_getName(property)];
    NSLog@("The propertyName is %@",propertyName);
}
Community
  • 1
  • 1
ader
  • 5,403
  • 1
  • 21
  • 26
  • Tahnks for your reply. But this block returns all propertyNames of the given view. For example withe a UILabel: lineSpacing, text, font, etc. What im am loooking for is the "variable (property) name" of the given label as defined in the header file. – Joe Even Jun 06 '12 at 14:03
  • If you want the ivars use class_copyIvarList, if you want the properties use class_copyPropertyList as above and if you want the methods use class_copyMethodList. I'm not sure what you're not understanding I'm afraid. Surely you would run this against your subclass to get the properties you defined there? It sounds like you're running this against the UILabel class when you should run it against your subclass X. – ader Jun 06 '12 at 14:41