Yymodel Source code Interpretation (ii) of the Nsobject+yymodel.h (4)

Source: Internet
Author: User

And then we continue to look down.

struct {    void *modelmeta;  /// < _yymodelmeta     void *model;       /// < ID (self) ///< Nsdictionary (JSON)} modelsetcontext;      

This is a C struct, in C void * equals the ID type in OC

So why use C's structure, the main use of the scenario is that we need to use multiple parameters at the same time, we can use the structure of C

/** Apply function for dictionary, to set the key-value pair to model. @param _key should not being nil, nsstring. @param _value should not be nil. @param _context _context.modelmeta and _context.model should not be nil. */Static voidModelsetwithdictionaryfunction (Const void*_key,Const void*_value,void*_context) {Modelsetcontext*context =_context; __unsafe_unretained _yymodelmeta*meta = (__bridge _yymodelmeta *) (context->Modelmeta); __unsafe_unretained _yymodelpropertymeta*propertymeta = [meta->_mapper Objectforkey: (__bridge ID)    (_key)]; __unsafe_unretained ID Model= (__bridge ID) (context->model);  while(Propertymeta) {if(propertymeta->_setter)        {Modelsetvalueforproperty (model, (__bridge __unsafe_unretained ID) _value, propertymeta); } Propertymeta= propertymeta->_next; };}

The main function of the above code is based on an ID type of _value an ID type of _key and _context structure information, social value into the corresponding attribute

/** Apply function for model property meta, to set dictionary to model. @param _propertymeta should not being nil, _yymodelpropertymeta. @param _context _context.model and _context.dictionary should not be nil. */Static voidModelsetwithpropertymetaarrayfunction (Const void*_propertymeta,void*_context) {Modelsetcontext*context =_context; //This dictionary is a JSON dictionary .__unsafe_unretained nsdictionary *dictionary = (__bridge nsdictionary *) (context->dictionary); __unsafe_unretained _yymodelpropertymeta*propertymeta = (__bridge _yymodelpropertymeta *) (_propertymeta); if(!propertymeta->_setter)return; ID value=Nil; //If the property maps multiple Jsonkey    if(propertymeta->_mappedtokeyarray) {        //This is the previous function, which is to remove the corresponding value based on the dictionary and the mapped Jsonkey, and to stop the traversal if it gets to the first value that is not emptyValue = Yyvalueformultikeys (dictionary, propertymeta->_mappedtokeyarray); } Else if(propertymeta->_mappedtokeypath) {Value= Yyvalueforkeypath (Dictionary, propertymeta->_mappedtokeypath); } Else{Value= [Dictionary objectforkey:propertymeta->_mappedtokey]; }        if(value) {__unsafe_unretained ID model= (__bridge ID) (context->model);    Modelsetvalueforproperty (model, value, Propertymeta); }}

The upper code is also an assignment function, in the case of a Propertymeta and context parameter, the implementation of the assignment method

The next method is primarily the core method of the mode to JSON model-to-dictionary, which is designed to preprocess the model

Note that this method simply preprocessed a NSObject object and did not go to the JSON object Apple rules the requirement to use Nsjsonserialization to convert to Jsonobject: The Nsarray object array element can only be nsstring, NSNumber, Nsnullnsdictionary object key must be Nsstringvalue can only be nsstring, NSNumber, nsnull the function of the incoming NSObject object for all types when the preprocessing: NSData>>>cannot be converted into jsonnsstring>>>Nsstringnsnumber>>>Nsnumbernsurl>>>nsstringnsattributedstring>>>nsstringnsdate>>>use DateFormat to convert to Nsstringnsarray first to determine if Nsarray can be json if possible, Direct return Nsarray If you cannot create a new Nsarray traversal before each element in Nsarray recursively parses the current element into NSString, NSNumber adds the parsed element to the array to return the new array Nssetnsset>>>Nsarray Walk Nsarray process nsdictionary like Nsarray custom NSObject class, non-foundation class will be the current entity class object>>>to reassemble property values using the Nsdictionary object>>> Nsdictionary Dictionary key-Value Final entity class object>>> Nsdictionary Objects

Yymodel Source code Interpretation (ii) of the Nsobject+yymodel.h (4)

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.