0

I have to convert a class object in NSDictionary

I have following class

@interface FoodMeal : NSObject<NSCoding,NSCopying>

@property (nonatomic,strong) NSString *mealType;
@property (nonatomic,strong) NSString *mealName;
@property (nonatomic,strong) NSArray *mealItems;
@property (nonatomic,strong) NSDate *mealDate;

@end

mealItems contains FoodItems which is a class object.

FoodItems has a variable Nutrition facts which intern a Class object.

Is there a efficient way to convert FoodMeal class to NSDictionary.

Thanks in advance.

Amit Kalghatgi
  • 357
  • 3
  • 16
  • see this once you get ideas http://stackoverflow.com/questions/19079862/converting-nsobject-to-nsdictionary – Anbu.Karthik Jul 20 '16 at 08:15
  • I have seen that solution...I have to add manual all the content to Dictionary? Is there any other efficient solution.. – Amit Kalghatgi Jul 20 '16 at 08:37
  • You could try [getting all the properties names](http://stackoverflow.com/questions/780897/how-do-i-find-all-the-property-keys-of-a-kvc-compliant-objective-c-object) and then using `valueForKey:` to get their value. – Droppy Jul 20 '16 at 08:53

3 Answers3

2

you can use jsonmodel

plz see this link https://github.com/jsonmodel/jsonmodel

ex:

download the jsonmodel import in your xcode porject

then

@interface FoodMeal : JSONModel

@property (nonatomic,strong) NSString *mealType;
@property (nonatomic,strong) NSString *mealName;
@property (nonatomic,strong) NSArray *mealItems;
@property (nonatomic,strong) NSDate *mealDate;

@end

make your class subclass of jsonmodel . jsonmodel super class is NSObject.

than when you want to convert your class object to Dictionary then use it like this

FoodMeal *ob = [FoodMeal alloc]init];

NSDictionary *dictob = [ob toDictionary];

toDictionary is method in JSONModel that return you NSDictionary;

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

Its very simple just write a method in your FoodMeal class

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];

    [mutableDict setValue:self.mealType forKey:@"KeyForMealType"];
    [mutableDict setValue:self.mealName forKey:@"KeyForMealName"];
    [mutableDict setValue:self.mealItems forKey:@"KeyForMealItem"];
    [mutableDict setValue:self.mealDate forKey:@"KeyForMealDate"];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

and after that you can use this method as

FoodMeal *food = [FoodMeal alloc] init];
 food.mealType = @"ABC";
 food.mealName = @"DEF"
 food.mealItems = @[@"Item 1", @"Item 2",@"Item 3",@"Item 4"];
 food.mealDate = [NSDate date];

 [food dictionaryRepresentation];
Waris Shams
  • 1,606
  • 1
  • 15
  • 27
  • 1
    hmmm means I have to keep creating Mutable Dictionary for each object... I have to deep dive into mealItems array.. as Item1 is again a class say Nutrition, Servings with 10-12 properties... – Amit Kalghatgi Jul 20 '16 at 09:38
  • its NSDictionary responsibility to handle NSArray. By the way what you want to do with dictionary of Object, may some other solution helps you out – Waris Shams Jul 20 '16 at 09:39
0

Try this:

#import <objc/runtime.h>


//Add this utility method in your class.
+ (NSDictionary *)dictionaryWithPropertiesOfObject:(id)obj {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    unsigned count;
    objc_property_t *properties = class_copyPropertyList([obj class], &count);

    for (int i = 0; i < count; i++) {
        NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
        [dict setObject:[obj valueForKey:key] ? [obj valueForKey:key] : @"" forKey:key];
    }

    free(properties);

    return [NSDictionary dictionaryWithDictionary:dict];
}
刘俊利
  • 358
  • 4
  • 12