title: How to dynamically determine Objective-C property type?
body: <p>I'm trying to dynamically determine the type of a property in Objective-C. Based on what I have read on this site and elsewhere, I believe I am doing the right thing. However, my code isn't working.</p>
  
  <p>The code snippet below demonstrates the problem. Attempting to get the property information for "backgroundColor" and "frame", both of which are valid properties of UIView, fails (class_getProperty() returns NULL):</p>
  
  <pre><code>id type = [UIView class];        
  objc_property_t backgroundColorProperty = class_getProperty(type, "backgroundColor");
  fprintf(stdout, "backgroundColorProperty = %d\n", (int)backgroundColorProperty); // prints 0
  
  objc_property_t frameProperty = class_getProperty(type, "frame");
  fprintf(stdout, "frameProperty = %d\n", (int)frameProperty); // prints 0
  </code></pre>
  
  <p>Enumerating the properties as described <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html" rel="nofollow">here</a> doesn't produce the expected results, either. The following code:</p>
  
  <pre><code>NSLog(@"Properties for %@", type);
  unsigned int outCount, i;
  objc_property_t *properties = class_copyPropertyList(type, &amp;outCount);
  for (i = 0; i &lt; outCount; i++) {
      objc_property_t property = properties[i];
      fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
  }
  </code></pre>
  
  <p>generates this output:</p>
  
  <pre><code>2012-03-09 13:18:39.108 IOSTest[2921:f803] Properties for UIView
  caretRect T{CGRect={CGPoint=ff}{CGSize=ff}},R,N,G_caretRect
  gesturesEnabled Tc,N
  deliversTouchesForGesturesToSuperview Tc,N
  skipsSubviewEnumeration Tc,N
  viewTraversalMark Tc,N
  viewDelegate T@"UIViewController",N,G_viewDelegate,S_setViewDelegate:
  inAnimatedVCTransition Tc,N,GisInAnimatedVCTransition
  monitorsSubtree Tc,N,G_monitorsSubtree,S_setMonitorsSubtree:
  backgroundColorSystemColorName T@"NSString",&amp;,N,G_backgroundColorSystemColorName,S_setBackgroundColorSystemColorName:
  userInteractionEnabled Tc,N,GisUserInteractionEnabled
  tag Ti,N,V_tag
  layer T@"CALayer",R,N,V_layer
  </code></pre>
  
  <p>Documented properties such as "backgroundColor", "frame", and others are missing, whereas undocumented properties like "caretRect" and "gesturesEnabled" are included.</p>
  
  <p>Any help would be very much appreciated. In case it is relevant, I'm seeing this behavior on the iOS simulator. I don't know if the same thing would happen on an actual device.</p>
  
  <p>Thanks,
  Greg</p>