title: ObjC get property name
body: <p>I've looking for a way to get a property name as StringValue from inside a method.</p>
  
  <p>Lets say:</p>
  
  <p>My class has X Subviews from the Type UILabel.</p>
  
  <pre><code>@property (strong, nonatomic) UILabel *firstLabel;
  @property (strong, nonatomic) UILabel *secondLabel;
  [...]
  </code></pre>
  
  <p>and so on.</p>
  
  <p>Inside the method foo, the views are iterated as followed:</p>
  
  <pre><code>-(void) foo 
  {
  
  for (UIView *view in self.subviews) {
   if( [view isKindOfClass:[UILabel class]] ) {
   /*
  codeblock that gets the property name.
  */
  
   }
  }
  }
  </code></pre>
  
  <p>The Result should be something like that:</p>
  
  <pre><code>THE propertyName(NSString) OF view(UILabel) IS "firstLabel"
  </code></pre>
  
  <p>I've tried <strong>class_getInstanceVariable</strong>, <strong>object_getIvar</strong> and <strong>property_getName</strong> without Success.</p>
  
  <p>For example, the code for:</p>
  
  <pre><code>[...]
  property_getName((void*)&amp;view)
  [...]
  </code></pre>
  
  <p>Returns: </p>
  
  <pre><code>&lt;UILabel: 0x6b768c0; frame = (65 375; 219 21); text = 'Something'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x6b76930&gt;&gt;
  </code></pre>
  
  <p>But i'm looking for this kind of result: "<strong>firstLabel</strong>" , "<strong>secondLabel</strong>" and so on.</p>
  
  <hr>
  
  <h1>Solved</h1>
  
  <p>As in the Reply of graver described the solution is:
  class_copyIvarList which returns the name of the Ivars.</p>
  
  <pre><code>Ivar* ivars = class_copyIvarList(clazz, &amp;count);
  NSMutableArray* ivarArray = [NSMutableArray arrayWithCapacity:count];
  for (int i = 0; i &lt; count ; i++)
  {
      const char* ivarName = ivar_getName(ivars[i]);
      [ivarArray addObject:[NSString  stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
  }
  free(ivars);
  </code></pre>
  
  <p>See posts:
  <a href="https://stackoverflow.com/a/2302808/1228534">https://stackoverflow.com/a/2302808/1228534</a>
  and
  <a href="https://stackoverflow.com/questions/2299841/objective-c-introspection-reflection">Objective C Introspection/Reflection</a></p>