LLModel

所属分类:自然语言处理
开发工具:Objective-C
文件大小:30KB
下载次数:0
上传日期:2015-03-31 12:29:08
上 传 者sh-1993
说明:  JSON的对象属性映射器
(Object Property Mapper for JSON)

文件列表:
LICENSE (1097, 2015-03-31)
LLModel.podspec (979, 2015-03-31)
LLModel (0, 2015-03-31)
LLModel\LLModel.h (597, 2015-03-31)
LLModel\LLModel.m (9890, 2015-03-31)
LLModel\PropertyUtil.h (270, 2015-03-31)
LLModel\PropertyUtil.m (2381, 2015-03-31)
LLModelExample.xcodeproj (0, 2015-03-31)
LLModelExample.xcodeproj\project.pbxproj (26101, 2015-03-31)
LLModelExample (0, 2015-03-31)
LLModelExample\Address.h (304, 2015-03-31)
LLModelExample\Address.m (820, 2015-03-31)
LLModelExample\AppDelegate.h (379, 2015-03-31)
LLModelExample\AppDelegate.m (2304, 2015-03-31)
LLModelExample\BaseModel.h (241, 2015-03-31)
LLModelExample\BaseModel.m (451, 2015-03-31)
LLModelExample\Default-568h@2x.png (18594, 2015-03-31)
LLModelExample\Default.png (6540, 2015-03-31)
LLModelExample\Default@2x.png (16107, 2015-03-31)
LLModelExample\Feed.h (344, 2015-03-31)
LLModelExample\Feed.m (953, 2015-03-31)
LLModelExample\LLModelExample-Info.plist (1464, 2015-03-31)
LLModelExample\LLModelExample-Prefix.pch (331, 2015-03-31)
LLModelExample\User.h (656, 2015-03-31)
LLModelExample\User.m (1561, 2015-03-31)
LLModelExample\ViewController.h (229, 2015-03-31)
LLModelExample\ViewController.m (3170, 2015-03-31)
LLModelExample\en.lproj (0, 2015-03-31)
LLModelExample\en.lproj\InfoPlist.strings (45, 2015-03-31)
LLModelExample\main.m (351, 2015-03-31)
LLModelExampleTests (0, 2015-03-31)
LLModelExampleTests\LLModelExampleTests-Info.plist (697, 2015-03-31)
LLModelExampleTests\LLModelExampleTests.h (256, 2015-03-31)
LLModelExampleTests\LLModelExampleTests.m (483, 2015-03-31)
LLModelExampleTests\en.lproj (0, 2015-03-31)
LLModelExampleTests\en.lproj\InfoPlist.strings (45, 2015-03-31)
Podfile (49, 2015-03-31)
... ...

# LLModel: Object Property Mapper for JSON (Deprecated, no longer maintained) LLModel is a library for mapping the JSON data to object properties. It works perfectly well alongside with [AFNetwoking][1]. LLModel: * supports recursive model initializations (see examples below) * supports primitive values such as bool, char, integer, float and others * handles NULL values gracefully * handles Date and URL values without any problem ### Version 1.1 * added default **(NSString *)description** method implementation. * iOS7 compatibility ## Example usage Say you have the following three models: * User * Address * Feed And the JSON data we will be receiving is going to be User which contains one Address and many Feeds. Here I will show only the User model (download the example to see other models). First of all, you have to subclass LLModel. ```` @interface User : LLModel @property (strong, nonatomic) NSNumber *userId; @property (strong, nonatomic) NSString *firstName; @property (strong, nonatomic) NSString *lastName; @property (strong, nonatomic) NSString *username; @property (nonatomic) BOOL publicProfile; @property (nonatomic) NSInteger loginCount; @property (strong, nonatomic) NSDate *createdAt; @property (strong, nonatomic) Address *address; @property (strong, nonatomic) NSMutableArray *feeds; @end ```` In the implementation file, you need to override the **initWithJSON:(id)JSON** method and define your mapping. Mapping is very basic, * The left side (dictionary keys) is your property names. * The right side (dictionary values) is the JSON keys. There is no need to worry about the types, LLModel will check the property types at runtime and assign the values accordingly. ```` @implementation User - (id)initWithJSON:(id)JSON { self = [super initWithJSON:JSON]; if(self) { self.mappingDateFormatter = [[NSDateFormatter alloc] init]; [self.mappingDateFormatter setDateFormat:@"dd.MM.yyyy"]; NSDictionary *mapping = @{@"userId":@"id", @"firstName":@"first_name", @"lastName":@"last_name", @"username":@"username", @"publicProfile":@"public_profile", @"loginCount":@"login_count", @"createdAt":@"created_at" @"address":@"address", @"feeds":@{@"key":@"feeds",@"type":@"Feed"}}; [self setValuesWithMapping:mapping andJSON:JSON]; // check if there are any errors if(self.mappingErrors.count > 0) { [self logAllMappingErrors]; } } return self; } @end ```` And finally, after you make a call to an API, initialize the LLModel instance (User) with the received JSON. That's it! ```` // The JSON from the folowing URL is generated specially for this project. NSURL *url = [NSURL URLWithString:@"http://www.dailypul.se/api/v1/test/llmodel"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { // Simply init the user with JSON, that's all! self.user = [[User alloc] initWithJSON:JSON]; Feed *feed = [self.user.feeds objectAtIndex:0]; // Log the model values NSLog(@"User description: %@", self.user.description); NSLog(@"User Address description: %@", self.user.address.description); NSLog(@"User Feed[0] description: %@", feed.description); NSLog(@"User dictionary: %@", [self.user reverseMapping]); NSLog(@"Feed dictionary: %@", [feed reverseMapping]); } failure:nil]; [operation start]; ```` ## Recursive Mapping In the above example the most interesting part is the mapping of the "address" and "feeds" properties. LLModel simply gets the values from the JSON and creates an Address object (Address model should also be a subclass of LLModel) and assigns to property. Feeds are a little bit different story. Since **feeds** property is defined as NSMutableArray (or it can be NSArray) LLModel can't know which type of objects it must contain. Therefore, in the mapping, you have to provide a dictionary with the keys: **key** and **type**. LLModel will then create the Feed objects and add them to NSarray and finally will assign to the property. ## Reverse Mapping If you want to receive the object values in the form of JSON data, you can simply call the **(NSDictionary *)reverseMapping** method. Well, if you wonder, reverse mapping is also recursive. In other words, if you call this on a User instance, you will also get Address and Feeds JSON inside the main JSON object. ## Error handling LLModel will add NSerror objects to self.mappingErrors if there are any errors while parsing the JSON. You can log the errors with the helper method **[self logAllMappingErrors]**. ## Batch initialization If you want to initalize array of JSON values in just one function call, instead of a for loop, here is the solution: ```` NSArray *cities = [City batch:[JSON valueForKey:@"data"]]; ```` ## Description (Print all property values) LLModel has a default implementation for **(NSString *)description**. This method prints all the properties with their values, so you can easily debug your application. ## Best Practice I recommend creating a BaseModel which will be a subclass of LLModel. All of your models then should subclass BaseModel instead of LLModel directly. One of the main reasons for this approach is because many of the dates you receive from JSON will have the same date format, so in BaseModel you can define the date formatter only once. ## Example Project Please download the example project to see how the entire system works. ## Contact Omer Faruk Gul [My LinkedIn Account][2] [1]: https://github.com/AFNetworking/AFNetworking [2]: http://www.linkedin.com/profile/view?id=44437676

近期下载者

相关文件


收藏者