Description: The model class template has filtered null values by default, with the exception of special case keyword ID name conflicts (need to manually remove the comment code). Mymessagemodel is the name of the example. You can pick up your own.
1. Create a class that inherits and NSObject itself for use when model data models are used. The corresponding property is then added in the. h file that returns data based on the interface document or JSON.
and copy the following model class template code. h file-(Instancetype) Initwithdictionary: (Nsdictionary *) dictionary; method to the data model class that you created. H.
2. In your own data model class. m file, copy the following model template class. m in the code to the class that you created. M.
Model class. h file
1 #import<Foundation/Foundation.h>2 3 @interfaceMymessagemodel:nsobject4 5 6 //Example property name, copy the data returned by the background interface yourself here7 8@property (nonatomic, strong) NSString *Namet; 9 Ten /** One * Init the model with dictionary A * - * @param dictionary dictionary - * the * @return Model - */ --(Instancetype) Initwithdictionary: (Nsdictionary *) dictionary; - + @end
Modell class. m file
1 #import "MyMessageModel.h"2 3 @implementationMymessagemodel4 5- (void) SetValue: (ID) value Forundefinedkey: (NSString *) Key {6 7 /*[Example] change property ID to ProductID8 *9 * if ([key isequaltostring:@ "id"]) {Ten * One * Self.productid = value; A * return; - * } - */ the - //Show undefined key - //NSLog (@ "%@.h has undefined key '%@ ', the key ' s type is '%@ '.", Nsstringfromclass ([Self class]), key, [value Clas S]); - } + -- (void) SetValue: (ID) value Forkey: (NSString *) Key { + A //Ignore null value at if([Value Iskindofclass:[nsnullclass]]) { - - return; - } - - [Super Setvalue:value Forkey:key]; in } - to-(Instancetype) Initwithdictionary: (Nsdictionary *) Dictionary { + - if([Dictionary Iskindofclass:[nsdictionaryclass]]) { the * if(self =[Super Init]) { $ Panax Notoginseng [self setvaluesforkeyswithdictionary:dictionary]; - } the } + A returnSelf ; the } + - @end
3. For rare cases encountered the interface returns JSON data with ID of the parameter and the system ID keyword conflict resolution.
Open the. M medium
/* [Example] change property ID to ProductID
if ([Key isequaltostring:@ "id"])
{
Self.productid = value;
< Span style= "color: #008000;" >return; }
< Span style= "color: #008000;" > * / Open the comment for this line. Change the id attribute name of the conflict in. h to < Span style= "color: #008080;" >productid
4. How do I use the Model class? Controller Import the model header file. H. in the data method returned by the network request, this calls the
Mymessagemodel *model = [[mymessagemodel alloc] initwithdictionary:data];
This allows you to create a data model.
iOS Development--model class template (filter null and ID)