Mantle's use of personal opinions

Source: Internet
Author: User

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?

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.