Git Product home page Git Product logo

nsobject-onsteroids's Introduction

NSObject-OnSteroids

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" implies that the object being passed through here will look like {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!

nsobject-onsteroids's People

Contributors

cameronwebbable avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nsobject-onsteroids's Issues

Object Description doesn't look pretty

Example:

    "imageURL: https://somewebsite.com/static/images/image-placeholder.png\ncategoryName: 
Other\nABC: 094922230331\nID: 106042\nanotherfield: ffff\ncategoryID: 91\nname: 
Appletters Game\nbrandID: \n",

Rename this sucker

In an oversight on my end, calling this project "CWSmartObject" isn't appropriate since it's a category on NSObject. Brainstorm and change zee name!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.