Analysis on YYModel of YYKit and yykityymodel

Source: Internet
Author: User

Analysis on YYModel of YYKit and yykityymodel
This article mainly analyzes the structure of the YYModel file to help you get familiar with and learn YYModel more quickly. If you have any questions, please correct me. Thank you. 1. YYCLassInfo file YYClassIVarInfo: Model YYClassMethodInfo for storing variables: Model YYClassPropertyInfo for storing methods: Model YYClassInfo for storing property: Model YYClassIvarInfo for storing class information: initialized through Ivar, assigning values to the fields to be saved is relatively simple. YYClassMethodInfo: Initialize using Method. Unlike Ivar, there are two types of typeEncoding, one is a parameter, and the other is a returned value. An example is printed, for example:

 

TypeEncoding of the parameters and returned values, YYClassPropertyInfo of the returned values, and the parameters are initialized through objc_property_t to save relevant information. YYClassInfo: Class or NSString * className can be used to initialize and save information about the related classes. 2. The NSObject + YYModel file is the core of YYModel and provides methods such as son-to-model conversion, dictionary-to-model conversion, model-to-NSData conversion, and model-to-Json conversion. 1. Two Models: _ YYModelPropertyMeta and _ YYModelMeta. _ YYModelPropertyMeta: Further encapsulates YYClassPropertyInfo and adds some operational or functional encapsulation. For example, whether it is a C number type, whether it supports KVC, whether it can be archived, and whether it has a custom ing dictionary. Another special feature is next. next is the pointer to the next _ YYModelPropertyMeta, which is used to process multiple attributes mapped to the same key. _ YYModelMeta: layer-1 parsing of YYClassInfo, ing the _ YYModelPropertyMeta --> mapper, all _ YYModelPropertyMeta --> allPropertyMetas corresponding to the key of each property, all the _ YYModelPropertyMeta that parse the keyPath, And _ YYModelPropertyMeta --> multiKeysPropertyMetas, ing mapper. count --> keyMappedCount, Model class type --> nsType, and whether the model supports conversion from dic to model, to dic And to class. 2. inline function 1) YYClassGetNSType: Obtain the Foundation Class type of the class 2) YYEncodingTypeIsCNumber: Determine whether YYEncodingType is a C number (Common Data type) 3) YYNSNumberCreateFromID: convert a value of the id type to the NSNumber type. This function is mainly used to process data of the string type. Several c functions are used, respectively: atop (const char * cstring) returns a double type, converts string to double isnan (double num) to determine whether it is a number, whether it is infinite or infinitely small isinf (double num) num is positive infinity returns 1, num is negative infinity returns-1 atoll (const char * cstring) returns a long int 4) YYNSDateFormString: the format time string function is applicable to several time formats of Google, Github, Apple, Facebook, Weibo, and Twitter. Different Callbacks are used to adapt to different formats through a block. The block array is used to implement different callbacks based on different scenarios. 5) YYNSBlockClass: or obtain the NSBlock type, and then confirm that the attribute type is 6) YYISODateFormatter: you can know the role from the name 7) YYValueForKeyPath and YYValueForMultiKeys: use key or keys to get value 8 from dic.) ModelCreateNumberFromProperty: Obtain Number from _ YYModelPropertyMeta (when the attribute is number ), use the runtime method objc_msgSend to send a message (getter) to the current model to obtain the number. 9) ModelSetNumberToProperty: set the number of the property through _ YYModelPropertyMeta (in the case that the property is a number). Set the value for the current model as above. 10) ModelSetValueForProperty: It can be seen from the name that this is the set property method. It determines the type of the current property stored in meta to initialize the value of the current property 11) modelSetWithDictionaryFunction: encapsulate ModelSetValueForProperty. It is better to use the CFDictionaryApplyFunction to parse the dictionary data to the model. 12) modelsetwithpropertymetstmrayfunction: resolve multiple keys corresponding to one property meta. 13) ModelToJSONObjectRecursive: The classification definition interface for converting model to json 3, NSObject, NSArray, and NSDictionary uses the inline function described above. To facilitate external calls, you can use this encapsulation to call these methods. For more information, see @ ibireme. https://github.com/ibireme/yymodel. In the YYClassIvarInfo, YYCLassMethodInfo, YYClassPropertyInfo all have a TypeEncoding attribute, the specific content of the relevant please see Type Encoding: official link: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW5 finally said written in the front of the macro definition, # define force_inline _ inline _ attribute _ (always_inline) can be seen from the literal meaning, which is the meaning of force inline. The inline keyword is only recommended for the compiler to perform inline expansion, not mandatory. In the gcc compiler, if the compilation optimization is set to O0, even inline functions will not be expanded inline unless forced inline. 1. inline is mainly used to replace the macro definition in expression form in C, because such a macro definition is actually similar to a function, but in the macro definition, only a simple symbol replacement is made, it cannot check the parameter validity, strictly check the compiler, and forcibly convert the returned value. Therefore, macro definition has a series of risks and limitations. Therefore, the inline keyword is displayed. Therefore, the inline function is also directly used (like a macro), without the call overhead, and the efficiency is relatively high. Inline will be expanded inline during file calling, but gcc will still generate an independent assembly code for the inline function for external files. The inline Function of gcc is global, and the file is expanded in inline mode. The external function is the same as the normal extern function. 2. static inline is also the same as inline. It is recommended that the compiler expand inline. gcc generally does not generate an independent assembly code for the static inline function, but when the function recursion: the function address is used (function pointer) and will not be expanded.

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.