Simplify iOS Models With Mantle – An Intro
来源:互联网 发布:淘宝类目手机怎么设置 编辑:程序博客网 时间:2024/05/21 19:09
转载自:http://spin.atomicobject.com/2014/06/23/ios-models-mantle/
等理解应用了之后,再翻译这篇文章。
Mantle is a framework that makes using iOS models ridiculously easy. It helps manage all the boilerplate associated with models including serialization, deserialization, and equality.
Let’s say we have an application that needs to create a user and it must contact a server to do so. Also, let’s say the server is written in Ruby.
- Create the Model
First, we need a User model to pass to the server. Here is what that model might look like:
@interface EKOCloudAPIProfile : NSObject @property (strong, nonatomic, readwrite) NSString *firstName;@property (strong, nonatomic, readwrite) NSString *lastName;@property (strong, nonatomic, readwrite) NSNumber *age;@property (strong, nonatomic, readwrite) NSNumber *weight;@property (strong, nonatomic, readwrite) NSDate *birthdate; - (BOOL)isEqual:(id)anObject;- (void)hash;
If we want to test our model later (of course we do), we should also implement
-isEqual:
, which of course requires us to also implement-hash
. Yay, more boilerplate code! It is really going to get tedious implementing that-isEqual:
method with all of the properties we have.Luckily, Mantle has MTLModel. All of our models should inherit from MTLModel. Oh, by the way, MTLModel implements
-isEqual:
,-hash
, and all of the methods fromNSCopying
for us. Here is our MTLModel:@interface EKOCloudAPIProfile : MTLModel <MTLJSONSerializing> @property (strong, nonatomic, readwrite) NSString *firstName;@property (strong, nonatomic, readwrite) NSString *lastName;@property (strong, nonatomic, readwrite) NSNumber *age;@property (strong, nonatomic, readwrite) NSNumber *weight;@property (strong, nonatomic, readwrite) NSDate *birthdate; @end
- Set Up Property Keys
Now that we have our MTLModel, we need to implement the only required method;
+JSONKeyPathsByPropertyKey
. This method is responsible for serializing variables in the casing the server wants them, and deserializes them into the properties we want. It’ll look something like this:+ (NSDictionary)JSONKeyPathByPropertyKey { return @{ @"firstName" : @"first_name", @"lastName" : @"last_name" };}
Since age, weight, and birthdate will look the same on both sides, we can emit them from the dictionary.
- Add Property Transformations
If we stopped here, our serialized JSON would contain an
NSDate
object for the birthdate. Another goodie from Mantle is custom JSON transformer methods. We can add custom serialization or deserialization for any property by using+<key>JSONTransformer
.We really want to serialize the birthdate property into a string, and we would expect the server to send us a string that we will then deserialize back into an
NSDate
. Here is our implementation:+ (NSValueTransformer *)birthdateJSONTransformer dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy/MM/dd"; return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *dateStr) { return [dateFormatter dateFromString:dateStr]; } reverseBlock:^(NSDate *date) { return [dateFormatter stringFromDate:date]; }];}
Using
+reversibleTransformerWithForwardBlock:reverseBlock:
fromMTLValueTransformer, we can serialize a property with forwardBlock and deserialize with reverseBlock. - Use the Model
Now that our model is looking all pretty, it is time to actually use it!
When we want to serialize our model to send in a request we use
[MTLJSONAdapter +JSONDictionaryFromModel:]
.When we want to deserialize our model from a response we use
[MTLJSONAdapter +modelOfClass:fromJSONDictionary:error:]
.Here is what our network layer might look like:
- (User)updateUser:(User)user { NSDictionary *userDict = [MTLJSONAdapter JSONDictionaryFromModel:user]; NSDictionary *updatedUser = [self.webClient postToPath:@"users.json" withParameters:userDict]; return [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:updatedUser error:nil];}
And that’s it! We have a model that can be easily serialized, deserialized, and compared to other objects. Be sure to checkout Mantle’s Github page for plenty more treats.
如果大家觉得还不够 还可以看看这几篇bolg
http://substantial.com/blog/2014/05/20/parsing-json-in-ios-with-mantle/
https://github.com/Mantle/Mantle
http://www.objc.at/mantle
- Simplify iOS Models With Mantle – An Intro
- Getting Started with Redux: An Intro
- IOS Dev Intro - AVCaptureSession with multiple previews
- Core Data with Mantle
- Core Data with Mantle
- Core Data with Mantle
- Mantle with CoreData
- iOS的Mantle实战
- ios Mantle使用小记
- ios Mantle使用小记
- iOS Mantle 详解
- iOS Mantle详解2
- iOS的Mantle实战
- Mantle For iOS
- IOS Intro
- IOS Intro
- Mantle
- Mantle
- Capture a Screen Shot
- wampserver2.5-apache2.4.9允许外部访问的配置
- C++定义动态数组
- 【差分约束】
- Codeforces Round #274 (Div. 2)
- Simplify iOS Models With Mantle – An Intro
- shiro参考项目
- 我国虚拟现实技术何时走进现实
- 利用Python&Qt进行图形界面编程(1)环境配置
- Android注解IOC框架【ViewInject】(一)
- HTML <img> 标签的 usemap 属性
- shell使用技巧
- Android与WebView的插件管理机制
- Cracking the coding interview--Q1.8