Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A category of NSObject that allows you to create objects from dictionary.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 BITObject.xcodeproj
Octocat-spinner-32 BITObject
Octocat-spinner-32 BITObjectTests
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
README.md

A category of NSObject that allows you to create objects from dictionary.

Getting Started

  1. Add NSObject+BITObject folder into your project.
  2. Import NSObject+BITObject.h.
  3. Start using the only method available for it.

Example Usage

Say you have a dictionary that if you output to the console looks like this:

{
    superheros =     (
                {
            age = 35;
            isDead = 1;
            name = Batman;
            powers =             (
                                {
                    elements =                     (
                        metal,
                        air
                    );
                    name = batmobile;
                },
                                {
                    elements =                     (
                        metal
                    );
                    name = batarang;
                }
            );
        },
                {
            age = 30;
            isDead = 0;
            name = Superman;
            powers =             (
                                {
                    elements =                     (
                        fire,
                        metal
                    );
                    name = "laser eye";
                },
                                {
                    elements =                     (
                        air
                    );
                    name = fly;
                }
            );
        }
    );
}

Before we can convert this dictionary into an object, we'll need to create classes and properties that correlate to the dictionary keys.

First, we need a base object that contains an array of superheros. So, we create a class called, for example, JusticeLeague with an array property that will contain the superheros.

@interface JusticeLeague : NSObject

@property (strong, nonatomic) NSArray *superheros;

@end

Then, the Superhero class.

@interface Superhero : NSObject

@property (strong, nonatomic) NSString *name;
@property (unsafe_unretained, nonatomic) NSInteger age;
@property (unsafe_unretained, nonatomic) BOOL isDead;
@property (strong, nonatomic) NSArray *powers;

@end

Finally, the Power class.

@interface Power : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSArray *elements;

@end

Now, we can start using it. First, we allocate and initialize an instance of JusticeLeague. Then, we call the setPropertyValuesWithDictionary method. And voila...we have ourselves a JusticeLeague object with all properties created from the dictionary values.

JusticeLeague *league = [[[JusticeLeague alloc] init] autorelease];
[league setPropertyValuesWithDictionary:dictionary ignoreMissingPropertyNames:YES];

NSLog(@"%@", [[[league superheros] objectAtIndex:0] name]); //This will be "Batman"
Something went wrong with that request. Please try again.