-
Notifications
You must be signed in to change notification settings - Fork 22
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.
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;
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]);
}];
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
NSString *type = [propertyEnumerator propertyTypeFromName:@"name" object:person];
// return "NSString" as type (Person class).
It return either its Type/Class as NSString or nil.