Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return all properties of an object in Objective-C

Is it possible to return a list of all the properties implemented by an object in Objective-C? I understand the properties are just getters and setters so I'm not sure how it would work. If this cannot be done, then is it possible to return all the selectors that an object will respond to? I'm looking for something similiar to the "methods" method in Ruby.

like image 597
Joe Cannatti Avatar asked Mar 09 '10 15:03

Joe Cannatti


2 Answers

// Get properties list    
objc_property_t* class_copyPropertyList(Class cls, unsigned int *outCount); 
// Get methods list
Method* class_copyMethodList(Class cls, unsigned int *outCount); 

The following code will output all methods implemented in UIImage class to console:

unsigned int count;
Method* methods = class_copyMethodList([UIImage class], &count);
for (size_t i = 0; i < count; ++i)
   NSLog([NSString stringWithCString:sel_getName(method_getName(methods[i]))]);
free(methods);
like image 194
Vladimir Avatar answered Oct 26 '22 11:10

Vladimir


I was actually trying out this yesterday and it is possible, however you cannot get everything from UIView. Take a look at the Objective-C Runtime Reference

like image 35
Tuomas Pelkonen Avatar answered Oct 26 '22 11:10

Tuomas Pelkonen