I want to create an Objective-C base class that performs an operation on all properties (of varying types) at runtime. Since the names and types of the properties will not always be known, how can I do something like this?
@implementation SomeBaseClass
- (NSString *)checkAllProperties
{
for (property in properties) {
// Perform a check on the property
}
}
EDIT: This would be particularly useful in a custom - (NSString *)description: override.
To expand on mvds’ answer (started writing this before I saw his), here’s a little sample program that uses the Objective-C runtime API to loop through and print information about each property in a class:
Output:
Note that this program needs to be compiled with ARC turned on.