2

How do i to convert NSObject to NSDictionary?

At first step i have converted NSDictionary to NSObject like,

QRCodeData *obj = [[QRCodeData alloc] initWithQRcodeData:myDictonary];

QRCodeData.h

@interface QRCodeData : NSObject
-(instancetype)initWithQRcodeData:(NSDictionary*)dictionary;
@end

QRCodeData.m

@implementation QRCodeData

-(instancetype)initWithQRcodeData:(NSDictionary*)dictionary
{
    self = [super init];
    if(self){

        self.name = dictionary[@"userName"];
        self.phoneNumber = dictionary[@"mobileNo"];

    }
    return self;

}
@end

I want my Dictionary from object, it is possible to get?

Please help and thanks in advance..

Krunal
  • 6,440
  • 21
  • 91
  • 155
  • Specifically [this answer](http://stackoverflow.com/a/10318659/299924) which assumes you use the same names for the properties of your object and the keys of your dictionary, which sounds like a good idea to me. – trojanfoe May 18 '16 at 07:43
  • you want Dictionary in same class or in other class? – Piyush May 18 '16 at 08:57

3 Answers3

3

Easiest way is to add this method in the QRCodeData class.

- (NSDictionary *)dictionaryValue
{
  return @{@"userName" : self.name, @"mobileNo" : self.phoneNumber};
}

If userName and phoneNumber could be nil you have to check that.

To call with

NSDictionary *dict = [obj dictionaryValue];
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Try these two lines `QRCodeData *codeData = [[QRCodeData alloc] initWithQRcodeData:@{@"userName" : @"John Doe", @"mobileNo" : @"1-234-5678"}];` `NSLog(@"%@", [codeData dictionaryValue]);` You should get the same dictionary as passed in the initializer. – vadian May 18 '16 at 08:23
3

you can simply get dictionary like,

  NSDictionary *dict = @{@"userName": obj.name ,@"mobileNo" : obj.phoneNumber };

here obj is QRCodeData's object.

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • What do i write in QRCodeData class? – Krunal May 18 '16 at 08:13
  • you not need to write anything in your `QRCodeData` class. you should have property in .h file like `name` and `phoneNumber` and when you want to convert it in dictionary in any class just import `QRCodeData.h` and make object of it and by that object you can make dictionary as mentioned in my answer – Ketan Parmar May 18 '16 at 09:06
2

You can use Key-Value Coding(KVC) for this purpose. First, provide class method for all keys you want to share:

+ (NSSet *)keysToCopy
{
    return [NSSet setWithObjects:@"userName", @"mobileNio", .....];
}

Then you can do something like in your init method:

for (key in [[self class] keysToCopy])
{
    [self setValue:dictionary[key] forKey:key];
}

and provide another method to revert it back to NSDictionary:

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *result = [NSMutableDictionary dictionary];
    for (key in [[self class] keysToCopy])
    {
        [result setObject:[self valueForKey:key] forKey:key];
    }
}

The only problem remains that not every property is compatible to NSDictionary storing.

This approach allows you to scale this solution to any Cocoa object and it doesn't require you to change anything but keysToCopy method in case if there are new properties to share.

Andriy
  • 2,767
  • 2
  • 21
  • 29