Giter Site home page Giter Site logo

prarik / shmodelobject Goto Github PK

View Code? Open in Web Editor NEW

This project forked from grevolution/shmodelobject

0.0 1.0 0.0 126 KB

Base model object which uses objective-c runtime to populate the instance variables (ivars and properties) of User created Model from NSDictionary objects.

License: MIT License

shmodelobject's Introduction

SHModelObject

SHModelObject is a utility model Base Class that uses objective-c runtime to assign the values to instance variables and properties of the model class from an NSDictionary, Which is a basic usecase when using webservices that return JSON response.

Build Status

Let say you have a WebService that serves you back the following Response:

{
	"user_name" : "Shan Ul Haq",
	"user_id" : 34567,
	"user_role" : "Author",
	"user_x_values" : [
		"abcd", 777, "efgh" , true, false
	]
}

You parse this response using a JSON library and convert this response into a NSDictionary. Now another task is to populate following User model Object

@interface User : NSObject {
	NSString *_userName;
	NSInteger _userId;
	NSString *userRole;
}

@property(nonatomic, strong) NSArray *userXValues;

@end

To populate an Object of User, you will have to write some initializer that will take a NSDictionary and will populate the ivars and properties of your model class one by one. and you will have to do this for all the model classes that you have in your project. you will be adding something like below to your class and implement the method.

- (User *)initWithDictionary:(NSDictionary *)responseDictionary;

Thats where SHModelObject comes in, its a Single class with basic purpose to reduce this effort and do the work for you. all you have to do is subclass your model class with SHModelObject, so the above class becomes:

@interface User : SHModelObject {
	NSString *_userName;
	NSInteger _userId;
	NSString *userRole;
}

@property(nonatomic, strong) NSArray *userXValues;

@end

and thats it. SHModelObject has a basic initializer that will take the NSDictionary and will populate all the ivars and properties for you.

just initialize the model with provided initializers and off you go.

User *u = [[User alloc] initWithDictionary:responseDictionary];

##How SHModelObject knows which value to assing to which instance variable?

SHModelObject compares the keys of NSDictionary with the ivar or property names in an efficient way. while comparing the names of keys with ivars it doesnt take _, - or into account (also the case doesnt matter). so

user_name OR USER_NAME OR USERNAME OR UserName will match with _userName OR userName OR UserName

you can override - (void)serializeValue:(id)value withKey:(id)key method if you want to a custom logic to parse a specific key/value pair. make sure to call [super serializeValue] for the values you want to parse by default.

- (void)serializeValue:(id)value withKey:(id)key
{
    if([key isEqualToString:@"numberVALUE"]) {
        _numberValue = value;
    } else {
        [super serializeValue:value withKey:key];
    }
}

##Parsing .NET JSON Dates to NSDate or NSTimeInterval

you can use kDateConversionOption to convert the .NET JSON Date Strings to either NSDate or NSTimeInterval or keep it as NSString and parse yourself.

##How to Use it.

1- Add the classes into your project

2- sublcass your models with SHModelObject

3- initialize using the povided initializers and pass the response NSDictionary ( initWithDictionary: and other variants )

4- override serializeValue if you want to parse a specific key/value your way.

5- thats it. off you go.

##Tasks Pending

  • adding to cocoapods.
  • adding support for custom instance variable types that are also subclasses of SHModelObject
  • implementing NSCoding for archiving/unarchiving the model objects.
  • implementing a deserializer for converting the object to NSDictionary.

##Contact Me

Shan Ul Haq (http://grevolution.me)

##License

SHModelObject is available under the MIT license. See the LICENSE file for more info.

shmodelobject's People

Contributors

grevolution avatar

Watchers

James Cloos avatar

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.