5

I would like to be able to use reflection on a classes within Objective C to determine what properties are available at run time.

I do something similar for classes right now using

NSString *str = NSStringFromClass([object class]);  

What I would like to do is use this result to go back to the class and see what properties are available as well as what type these properties are.

bigtunacan
  • 4,873
  • 8
  • 40
  • 73
  • why do you not use protocols to make sure the actual property / method is available in runtime for certain objects? – holex Jul 07 '14 at 10:23
  • Properties are determined at runtime based on the database schema so a dynamic method call is less code and more flexible for my particular use case. Also; it is interesting how you down vote me even though you have absolutely no idea what I'm trying to accomplish. – bigtunacan Jul 07 '14 at 16:10
  • no hard feelings. I highly doubt your database is such "dynamic" and that'd be the only and omnipotent solution. – holex Jul 07 '14 at 16:17
  • Like I said; you have no idea the context of what I was doing at the time. I was not building a one use data access layer. I created a drop in object relational mapper for use with SQLite. It is essentially ActiveRecord for iOS. This then allows automatic access to any table for what we consider to be "most-likely" needed behavior. If the developer needs something above and beyond than they are able to override behavior by defining a protocol only when it is actually needed. This saved us 3 months of work on the first project we used it on; that included time to build the library itself. – bigtunacan Jul 07 '14 at 17:27
  • good luck with your project! – holex Jul 07 '14 at 17:48

1 Answers1

9

May be this will help:

You can get the list of properties in a class using class_copyPropertyList

objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)

and then from each property, you can get the property name using property_getName function and the property attributes using the property_getAttributes function (if you need to filter read-write properties).

chx
  • 11,270
  • 7
  • 55
  • 129
MrWaqasAhmed
  • 1,479
  • 12
  • 12
  • 2
    Check out the [Objective-C Runtime Programming Guide](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW24) and the [Objective-C Runtime Reference](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html) for more information. – rob mayoff Nov 15 '12 at 07:42
  • Thanks to both of you for the answer to my direct question, but also to the extremely informative guides that provided a lot of additional information I was wanting as well. – bigtunacan Nov 18 '12 at 15:05