Quick Tutorial

If you don't want to follow the steps, you can download the final SampleProject and have a look. The magic is in the AppDelegate.m and the SampleProjectTests.m files.

If you prefer a short (ca. 5 minutes) tutorial, here you go:

  1. Create a new Xcode project called "SampleProject". (For example a OS X or iOS Application. You might want to select "Objective-C" as language.)
  2. Create a simple model in the JSON Class Generator (download the JSON Class Generator) editor window. Like the one from the screenshot:
  3. Press the Export Code button and save the files inside your Xcode project folder. (Maybe within a folder API)
  4. In Xcode Add Files to "SampleProject"... (the "+" button in the lower left corner of the project's window) and choose the API folder.
  5. Add #import "Api.h" to the AppDelegate.m
  6. Put the following code into the applicationDidFinishLaunching: method (or the application:didFinishLaunchingWithOptions: method, if you created an iOS project):
        NSDictionary *personJsonDict = @{ @"name" : @"John",
                                          @"age" : @12,
                                          @"gender" : @"male"
        Person *person = [Person personWithDict:personJsonDict apiError:nil];
        NSLog(@"%@", person);
  7. Run the project. You should see the following output (besides the project name and the time stamp) in the debugger console:
    Person { name = "John", age = 12, gender = GenderMale }

For your convenience, the description method of all models are already implemented in the generated code. The printed object should look similar to what you expected. Note, that the String "male" was converted to an enum value GenderMale, which consists of the enum type name as prefix and the actual value as suffix.

From here you can try building your own models or you can have a look at the SampleProject now, which contains some unit tests with more examples (using methods like toDict and api_toJson).