Latest 0.0.5
Homepage https://github.com/cameronwebbable/NSObject-OnSteroids
License MIT
Platforms ios , requires ARC
Authors

Allows you to create objects from dictionaries and just handles a bunch of other annoying tasks. Inspired by code that I wrote while still at Sqwiggle. NSObject-OnSteroids is derieved from SQObject.{h,m} in the Sqwiggle iOS SDK

Benefits

  • Rapidly map dictionary to object (helpful for dealing with dictionaries from REST APIs)
  • Export object to dictionary (helpful for when you are needed to post object to Rest APIs)
  • Encoding/Decoding for Coder baked right in.

How to Install

Add pod 'NSObject-OnSteroids' to your podfile, then import <NSObject-OnSteroids/NSObject-OnSteroids.h>. Sample code is available under subdirectory "NSObject-OnSteroidsExample".

How to Use

Create a new class that you’d like to map to. For this example, I’m going to create an object called "Book", and use the following code in .h and .m files, respectively:

Objective-C

//Book.h

#import <NSObject-OnSteroids/NSObject-OnSteroids.h>

@interface Book : NSObject

@property (nonatomic, retain) NSString *title;
@property (nonatomic, copy)   NSDictionary *someDictionary;

@end
//Book.m
#import "Book.h"

@implementation Book

-(NSDictionary *)modelDefinition
{
    return @{@"title" : @"title", @"someDictionary": @"dictKey.importantDictionary"};
}

@end

Swift (note: objects have to inheret NSObject. Sorry :-/. I’ll update this later)

//Objective-C Bridging Header
...
#import <NSObject-OnSteroids/NSObject-OnSteroids.h>
//Book.swift
class Book : NSObject {
    ... 
    override func modelDefinition() -> [NSObject : AnyObject]! {
        return ["title" : "title", "someDictionary" : "dictKey.importDictionary"]
    }
}

You might be wondering… "WTF IS THAT MODEL DEFINITION CRAP?". It’s a method you need to override in this object so it knows which keys map to what property. The format for modelDefinition is

{"iOS Class Property" : "Dictionary KeyPath"}

Also make note of @"dictKey.importantDictionary". If the concept of KeyValuePath is fuzzy for you, basically adding the "." allows you to traverse a dictionary to get any childern. In this case, {dictKey": {"importantDictionary" : {"blah" : "blah"}}}

Now that we have the object all handled, let’s go ahead and import Book.h elsewhere and initialize a Book object:

Objective-C

//SomeOtherClass.m
#import "Book.h"

...

Book *book = [Book objectWithDictionary:@{@"title": @"How Now Brown Cow", @"dictKey" : @{@"importantDictionary" : @{@"blah" : @"hah"}}];
NSLog(@"Book Object: %@ ", book);
NSLog(@"Book Title: %@", book.title);
NSLog(@"Book Dictionary %@", book.someDictionary);

Swift

//SomeOtherClass.swift
import Book

...

var Book : Book = Book(dictionary: ["title": "How Now Brown Cow", "dictKey" : ["importantDictionary" : ["blah" : "blah"]]])

println("Book Object: (book)")
println("Book Title: (book.title)")
println("Book Dictionary: (book.someDictionary)")

As you can now see, the given object has been mapped appropriately to each respective object. SWEET.

Let’s say I want to be able to get a dictionary format of the object I’ve just created. Simply do:

Objective-C

[book dictionaryFormat];

Swift

book.dictionaryFormat()

And you will be returned a dictionary of all properties and their respective values:

{
    dictKey = {"importantDictionary" : {"blah" : "hah"}};
    title = "How Now Brown Cow";
}

Sweet Jesus! Effectively removes the need for you to write any code to create a nicely formatted dictionary for posting, because it already knows what fields your API is using!

Feedback?

Open a GH Issue. Happy to discuss questions. This is still quite early and there’s a number of other ways I could go.

ToDo:

  • Tests!

Latest podspec

{
    "name": "NSObject-OnSteroids",
    "version": "0.0.5",
    "summary": "Allows you to create objects from dictionaries and just handles a bunch of other annoying tasks. Inspired by code in Sqwiggle for iOS SDK.",
    "homepage": "https://github.com/cameronwebbable/NSObject-OnSteroids",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Cameron Webb": ""
    },
    "social_media_url": "http://twitter.com/webbc",
    "platforms": {
        "ios": null
    },
    "source": {
        "git": "https://github.com/cameronwebbable/NSObject-OnSteroids.git",
        "tag": "0.0.5"
    },
    "source_files": [
        "NSObjectOnSteroids",
        "NSObjectOnSteroids/**/*.{h,m}"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This