Skip to content

Property Enumerator

Patrick-Kladek edited this page May 25, 2017 · 3 revisions

This class extreacts all properties from a given object an enumerates them in a block.

Enumerate Properties

Use the following method to enumerate all properties of a given Object.

- (void)enumerateProperties:(Class)objectClass allowed:(NSString *)allowed block:(void (^)(NSString *type, NSString *name))callbackBlock;

Example:

This Example enumerates all properties of a "Person" object

Person *person = [[Person alloc] init];
[person setName:@"John"];
[person setBirthday:[NSDate date]];

[propertyEnumerator enumerateProperties:[person class] allowed:nil block:^(NSString *type, NSString *name) {
	NSLog(@"[%@] %@=%@", type, name, [person valueForKey:name]);
}];

Property Type From Name

Use the following method if you need the type of a property when you only have its name.

- (NSString *)propertyTypeFromName:(NSString *)name object:(NSObject *)obj

Example

NSString *type = [propertyEnumerator propertyTypeFromName:@"name" object:person];
// return "NSString" as type (Person class).

It return either its Type/Class as NSString or nil.

Clone this wiki locally