MJExtension is a set of ultra-lightweight frameworks that convert between dictionaries and models.
The functions that MJExtension can complete are
dictionary (JSON) --> model (Model)
model (Model) --> dictionary (JSON)
dictionary array (JSON Array) --> Model Array
Model Array --> Dictionary Array (JSON Array)
Detailed usage mainly refer to the various functions in main.m and
the difference between MJExtension and JSONModel, Mantle and other frameworks
1. Conversion rate:
The latest test shows that: MJExtension > JSONModel > Mantle
, developers can also test by themselves
2. Specific usage:
JSONModel:
requires all model classes to inherit from JSONModel base class
Mantle:
requires all model classes to inherit from MTModel base class Class
MJExtension:
Does not require your model class to inherit any special base class, no pollution, no intrusion
How to use MJExtension
Method 1: cocoapods import: pod 'MJExtension'
Method 2: Manual import:
Drag all the source code in the MJExtensionExample/MJExtensionExample/MJExtension folder into the project
Import the main header file:
#import "MJExtension.h" MJExtension.h MJConst.h MJConst.m MJFoundation.h MJFoundation.m MJIvar.h MJIvar.m MJType.h MJType.m NSObject+MJCoding.h NSObject+MJCoding.m NSObject+MJIvar.h NSObject+MJIvar.m NSObject+MJKeyValue.h NSObject+MJKeyValue.m
1. The simplest dictionary conversion model
typedef enum { SexMale, SexFemale} Sex; @interface User : NSObject @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *icon; @property (assign, nonatomic) int age; @property (assign, nonatomic) double height; @property (strong, nonatomic) NSNumber *money; @property (assign, nonatomic) Sex sex; @end NSDictionary *dict = @{ @"name" : @"Jack", @"icon" : @"lufy.png", @"age" : @20, @"height" : @"1.55", @"money" : @100.9, @"sex" : @(SexFemale) }; // Convert dictionary to User model User *user = [User objectWithKeyValues:dict]; NSLog(@"name=%@, icon=%@, age=%d, height=%@, money=%@, sex=%d", user.name, user.icon, user.age, user.height, user.money, user.sex); // name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1
Core code 1:
[User objectWithKeyValues:dict]
2. Nested models in models
@interface Status : NSObject /** Weibo text content */ @property (copy, nonatomic) NSString *text; /** Weibo author */ @property (strong, nonatomic) User *user; /** Reposted Weibo */ @property (strong, nonatomic) Status *retweetedStatus; @end NSDictionary *dict = @{ @"text" : @"Yes, the weather is really nice today!", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" }, @"retweetedStatus" : @{ @"text" : @"The weather is really nice today!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } } }; // Convert dictionary to Status model Status *status = [Status objectWithKeyValues:dict]; NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon); // text=Yeah, the weather is really nice today! , name=Jack, icon=lufy.png NSString *text2 = status.retweetedStatus.text; NSString *name2 = status.retweetedStatus.user.name; NSString *icon2 = status.retweetedStatus.user.icon; NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2); // text2=The weather is great today! , name2=Rose, icon2=nami.png
core code 2
[Status objectWithKeyValues:dict]
3. There is an array attribute in the model, and the array must contain other models
@interface Ad : NSObject @property (copy, nonatomic) NSString *image; @property (copy, nonatomic) NSString *url; @end @interface StatusResult : NSObject /** Stores a bunch of Weibo data (all of them are Status models) */ @property (strong, nonatomic) NSMutableArray *statuses; /** Stores a bunch of advertising data (all of which are Ad models) */ @property (strong, nonatomic) NSArray *ads; @property (strong, nonatomic) NSNumber *totalNumber; @end @implementation StatusResult // The purpose of this method is to tell the MJExtension framework what model is installed in the statuses and ads arrays /* + (NSDictionary *)objectClassInArray{ return @{ @"statuses" : [Status class], @"ads" : [Ad class] }; } + (Class)objectClassInArray:(NSString *)propertyName{ if ([propertyName isEqualToString:@"statuses"]) { return [Status class]; } else if ([propertyName isEqualToString:@"ads"]) { return [Ad class]; } return nil;} */ // This method is less intrusive and polluting than the above two methods, because there is no need to import the header files of Status and Ad + (NSDictionary *)objectClassInArray{ return @{ @"statuses" : @"Status", @"ads" : @"Ad" }; } @end NSDictionary *dict = @{ @"statuses" : @[ @{ @"text" : @"The weather is really nice today!", @"user" : @{ @"name" : @"Rose", @"icon" : @"nami.png" } }, @{ @"text" : @"Going to travel tomorrow", @"user" : @{ @"name" : @"Jack", @"icon" : @"lufy.png" } } ], @"ads" :@[ @{ @"image" : @"ad01.png", @"url" : @"http://www.ad01.com" }, @{ @"image" : @"ad02.png", @"url" : @"http://www.ad02.com" } ], @"totalNumber" : @"2014" }; // Convert dictionary to StatusResult model StatusResult *result = [StatusResult objectWithKeyValues:dict]; NSLog(@"totalNumber=%@", result.totalNumber); // totalNumber = 2014 // print the model properties in the statuses array for (Status *status in result.statuses) { NSString *text = status.text; NSString *name = status.user.name; NSString *icon = status.user.icon; NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);} // text=The weather is great today! , name=Rose, icon=nami.png // text=Going to travel tomorrow, name=Jack, icon=lufy.png // print the model properties in the ads array for (Ad *ad in result.ads) { NSLog(@"image=%@, url=%@", ad.image, ad.url);} // image=ad01.png, url=http://www.ad01.com // image=ad02.png, url=http://www.ad02.com
Core code 3:
Implement + (NSDictionary *)objectClassInArray method inside the model
[StatusResult objectWithKeyValues:dict]
4. The attribute name in the model is not the same as the key in the dictionary (or multi-level mapping is required)
@interface Bag : NSObject @property (copy, nonatomic) NSString *name; @property (assign, nonatomic) double price;@end@interface Student : NSObject @property (copy, nonatomic) NSString *ID; @property (copy, nonatomic) NSString *desc; @property (copy, nonatomic) NSString *nowName; @property (copy, nonatomic) NSString *oldName; @property (copy, nonatomic) NSString *nameChangedTime; @property (strong, nonatomic) Bag *bag; @end @implementation Student // The purpose of this method is to tell the MJExtension framework that the attribute name in the model corresponds to which key of the dictionary + (NSDictionary *)replacedKeyFromPropertyName{ return @{ @"ID" : @"id", @"desc" : @"desciption", @"oldName" : @"name.oldName", @"nowName" : @"name.newName", @"nameChangedTime" : @"name.info.nameChangedTime", @"bag" : @"other.bag" }; } @end NSDictionary *dict = @{ @"id" : @"20", @"desciption" : @"孩子", @"name" : @{ @"newName" : @"lufy", @"oldName" : @"kitty", @"info" : @{ @"nameChangedTime" : @"2013-08" } }, @"other" : @{ @"bag" : @{ @"name" : @"Small bag", @"price" : @100.7 } } }; // Convert dictionary to Student model Student *stu = [Student objectWithKeyValues:dict]; // print the properties of the Student model NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime); // ID=20, desc=孩子, oldName=kitty, nowName=lufy, nameChangedTime=2013-08 NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price); // bagName=Small bag, bagPrice=100.700000
Core code 4:
Implement + (NSDictionary *)replacedKeyFromPropertyName method inside the model
[Student objectWithKeyValues:dict]
5. Convert an array of dictionaries to an array of models
NSArray *dictArray = @[ @{ @"name" : @"Jack", @"icon" : @"lufy.png", }, @{ @"name" : @"Rose", @"icon" : @"nami.png", } ]; // Convert the dictionary array to the User model array NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray]; // Print the User model properties in the userArray array for (User *user in userArray) { NSLog(@"name=%@, icon=%@", user.name, user.icon);} // name=Jack, icon=lufy.png // name=Rose, icon=nami.png
Core code 5:
[User objectArrayWithKeyValuesArray:dictArray]
6. Convert a model to a dictionary
// create a new model User *user = [[User alloc] init]; user.name = @"Jack"; user.icon = @"lufy.png"; Status *status = [[Status alloc] init]; status.user = user; status.text = @"I'm in a good mood today!"; // Convert the model to a dictionary NSDictionary *statusDict = status.keyValues; NSLog(@"%@", statusDict); /*{ text = "I'm in a good mood today!"; user = { icon = "lufy.png"; name = Jack; }; }*/ // Model for multi-level mapping Student *stu = [[Student alloc] init]; stu.ID = @"123"; stu.oldName = @"rose"; stu.nowName = @"jack"; stu.desc = @"handsome"; stu.nameChangedTime = @"2018-09-08"; Bag *bag = [[Bag alloc] init]; bag.name = @"Small bag"; bag.price = 205; stu.bag = bag; NSDictionary *stuDict = stu.keyValues;NSLog(@"%@", stuDict); /* { desciption = handsome; id = 123; name = { info ={ nameChangedTime = "2018-09-08"; }; newName = jack; oldName = rose; }; other = { bag ={ name = "Small bag"; price = 205; }; }; } */
Core code 6:
status.keyValues、stu.keyValues
7. Convert an array of models to an array of dictionaries
// create a new model array User *user1 = [[User alloc] init]; user1.name = @"Jack"; user1.icon = @"lufy.png"; User *user2 = [[User alloc] init]; user2.name = @"Rose"; user2.icon = @"nami.png"; NSArray *userArray = @[user1, user2]; // Convert the model array to an array of dictionaries NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray]; NSLog(@"%@", dictArray); /*( { icon = "lufy.png"; name = Jack; }, { icon = "nami.png"; name = Rose; } )*/
Core code 7:
[User keyValuesArrayWithObjectArray:userArray]
Original link: http://www.jianshu.com/p/93c242452b9b