TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jun 2016 |
Maintained by Shan Ul Haq.
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.
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];
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];
}
}
you can use kDateConversionOption
to convert the .NET JSON Date Strings to either NSDate
or NSTimeInterval
or keep it as NSString
and parse yourself and also you can define kInputDateFormat
to specify your input date format (JSON format, .NET Simple or .NET with timezone)
SHModelObject
you dont have to do anything :). SHModelObject
automatically handles it. checkout the sample code.
for example, following JSON
{
"name" : "Shan Ul Haq",
"person_id" : 123,
"image" : {
"image_id" : 234,
"image_url" : "http://image_url",
"orientation" : "portrait"
}
}
will be automatically parsed into following object:
@interface Person : SHModelObject
@property(nonatomic, strong) NSString *name;
@property(nonatomic) int personId;
@property(nonatomic, strong) Image *image;
@end
where Image object is also a SHModelObject
@interface Image : SHModelObject
@property(nonatomic) int imageId;
@property(nonatomic, strong) NSString *imageUrl;
@property(nonatomic, strong) NSString *orientation;
@end
SHModelObject
similar to parsing SHModelObject
instance variables, arrays can be handled too. you need to specify the mapping which will define that the JSON array consist of which object type.
for example, if you have following JSON
{
"aKey" : "aValue",
"arrayOfModels" : [
{
"modelId" : 2,
"modelName" : "My Model 2",
"modelType" : "My Model Type 2"
},{
"modelId" : 3,
"modelName" : "My Model 3",
"modelType" : "My Model Type 3"
},{
"modelId" : 4,
"modelName" : "My Model 4",
"modelType" : "My Model Type 4"
}
]
}
}
which translates to following objects.
@interface MyObject : SHModelObject
@property(nonatomic, strong) NSString *aKey;
@property(nonatomic, strong) NSArray *arrayOfModels;
@end
@interface AModel : SHModelObject
@property(nonatomic) int modelId;
@property(nonatomic, strong) NSString *modelName;
@property(nonatomic, strong) NSString *modelType;
@end
you can convert json like this:
// key is the variable name and value is the class name.
NSDictionary *mappingDictionary = @{@"arrayOfModels" : "AModel"};
MyObject *myObject = [MyObject objectWithDictionary:dictionary mapping:mappingDictionary];
Realm support out of the box. SHRealmObject
is a sublclass of RLMObject
from Realm. If you want to use both SHModelObject to parse your JSON responses and have RLMObject to be used with Realm database. SHRealmObject
is they class you need.
SHRealmObject
is identical in functionality as SHModelObject
but also confirms with RLMObject
. that means the it also confirms with the restriction that RLMObject
class has. (e.g. you cannot use NSDictionary
objects. and instead of using NSArrays
you will use RLMArray<T>
objects).
RLMArray<T>
objects will also be parsed automatically for you based on the mapping dictionary provided. (check out parsing arrays section above)
Following is a simple example.
@interface Person : SHRealmObject
@property NSString *name;
@property int age;
@property RLMArray<Car> *cars;
@end
@implementation Person
@end
////
@interface Car : SHRealmObject
@property NSString *model;
@end
@implementation Car
@end
and to add an object in Realm database.
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
NSDictionary *d = @{
@"nAme" : @"Shan Ul Haq",
@"_AGE" : @26,
@"cars" : @[ @{@"moDEL" : @"Honda"}, @{@"model" : @"Toyota"} ]
};
Person *p = [Person objectWithDictionary:d mappings:@{ @"cars" : @"Car" }];
[realm addObject:p];
}];
1- add the files
Using Cocoapods
pod 'SHModelObject/Core'
in your Podfile if you just want to use SHModelObject
pod 'SHModelObject/Realm'
in your Podfile if you want to use SHRealmObject
with Realm.pod 'SHModelObject'
in your Podfile if you want to use both SHModelObject
and SHRealmObject
.Manual
2- sublcass your models with SHModelObject
or SHRealmObject
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.
SHModelObject
NSCoding
for archiving/unarchiving the model objects.SHModelObject
objects.Shan Ul Haq (http://grevolution.me)
SHModelObject
is available under the MIT license. See the LICENSE file for more info.