Mantle's use of personal opinions
The previous month, I came into contact with Mantle. Because the project adopts the MVC design model, it is also crucial to choose a good model. Let's first introduce the use of Mantle.
First, define the data model:
@ Property (nonatomic, copy) NSString * address;
@ Property (nonatomic, copy) NSString * contact_info;
@ Property (nonatomic, copy) NSString * contact_name;
@ Property (nonatomic, assign) NSInteger id;
@ Property (nonatomic, copy) NSString * oppen_time;
@ Property (nonatomic, copy) NSString * project_id;
@ Property (nonatomic, copy) NSString * project_name;
@ Property (nonatomic, copy) NSString * project_situation;
@ Property (nonatomic, copy) NSString * title_name;
@ Property (nonatomic, copy) NSString * url;
Then, implement the following methods in the. m file:
+ (NSDictionary *) JSONKeyPathsByPropertyKey {
Return @{};
}
Use
_ BiddingModel = [MTLJSONAdapter modelOfClass: [TTBiddingModel class] fromJSONDictionary: dic error: nil];
What will return? I will talk about it later.
Reference to Why sing iOS6.0 selected Mantle http://www.cocoachina.com/ios/20141016/9931.html
JSONKeyPathsByPropertyKey this Protocol method is to serialize the content in the returned json
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return
@{
@
"identifier"
: @
"id"
,
@
"displayDiscription"
: @
"description"
,
@
"thisIsANewShit"
: @
"newShit"
,
@
"creativeProduct"
: @
"copyToChina"
,
@
"betterPropertyName"
: @
"m_wired_propertyName"
}
}
Automatically convert attribute names. So why do I return {}? Let's take a look at the source code of mantle. First, it defines such an attribute.
@ Property (nonatomic, copy, readonly) NSDictionary * JSONKeyPathsByPropertyKey;
Now _ JSONKeyPathsByPropertyKey is the + (NSDictionary *) JSONKeyPathsByPropertyKey {} We just used {};
_ JSONKeyPathsByPropertyKey = [[modelClass JSONKeyPathsByPropertyKey] copy];
In the init Method
-(Id) initWithModel :( MTLModel <MTLJSONSerializing> *) model {
NSParameterAssert (model! = Nil );
Self = [super init];
If (self = nil) return nil;
_ Model = model;
_ ModelClass = model. class;
_ JSONKeyPathsByPropertyKey = [[model. class JSONKeyPathsByPropertyKey] copy];
Return self;
}
Here, my cocopods Mantle is version 1.5, and the latest version 2.0.2 removes the following method:
-(NSString *) JSONKeyPathForPropertyKey :( NSString *) key {
NSParameterAssert (key! = Nil );
Id JSONKeyPath = self. JSONKeyPathsByPropertyKey [key];
If ([JSONKeyPath isEqual: NSNull. null]) return nil;
If (JSONKeyPath = nil ){
Return key;
} Else {
Return JSONKeyPath;
}
}
Therefore, this is why I returned {} in the Protocol method. It should be clear here. During serialization, it will automatically match the property defined by property. Is it convenient?