priore

Set property values ​​from a NSDictionary

Jun 4th, 2013
1,157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NSObject+NSDictionary.h
  3. //
  4. @interface NSObject (NSDictionary)
  5. - (void)setValuesWithDictionary:(NSDictionary*)dict;
  6. @end
  7.  
  8. //
  9. //  NSObject+NSDictionary.m
  10. //
  11. #import "NSObject+NSDictionary.h"
  12. #import <objc/runtime.h>
  13. @implementation NSObject (NSDictionary)
  14. - (void)setValuesWithDictionary:(NSDictionary*)dict
  15. {
  16.     unsigned int count = 0;
  17.     objc_property_t *properties = class_copyPropertyList([self class], &count);
  18.     for (int i = 0; i < count; ++i) {
  19.         objc_property_t property = properties[i];
  20.         NSString *name = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
  21.         if ([dict.allKeys indexOfObject:name] != NSNotFound) {
  22.             NSArray *propertyAttributes = [[[[NSString alloc] initWithUTF8String:property_getAttributes(property)] autorelease] componentsSeparatedByString:@","];
  23.             if ([propertyAttributes indexOfObject:@"R"] == NSNotFound) {
  24.                 if ([propertyAttributes indexOfObject:@"T@\"NSString\""] != NSNotFound) {
  25.                     [self setValue:[dict objectForKey:name] forKey:name];
  26.                 } else if ([propertyAttributes indexOfObject:@"Ti"] != NSNotFound) { // integer
  27.                     [self setValue:@([[dict objectForKey:name] integerValue]) forKey:name];
  28.                 } else if ([propertyAttributes indexOfObject:@"Tf"] != NSNotFound) { // float
  29.                     [self setValue:@([[dict objectForKey:name] floatValue]) forKey:name];
  30.                 } else if ([propertyAttributes indexOfObject:@"Td"] != NSNotFound) { // double
  31.                     [self setValue:@([[dict objectForKey:name] doubleValue]) forKey:name];
  32.                 } else if ([propertyAttributes indexOfObject:@"Tl"] != NSNotFound) { // long
  33.                     [self setValue:@([[dict objectForKey:name] longValue]) forKey:name];
  34.                 } else if ([propertyAttributes indexOfObject:@"Ts"] != NSNotFound) { // short
  35.                     [self setValue:@([[dict objectForKey:name] shortValue]) forKey:name];
  36.                 } else if ([propertyAttributes indexOfObject:@"Tc"] != NSNotFound) { // bool YES/NO (char ???)
  37.                     [self setValue:@([[dict objectForKey:name] boolValue]) forKey:name];
  38.                 }
  39.             }
  40.         }
  41.     }
  42.     free(properties);
  43. }
  44. @end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×