iOS Development Tips-using macro-simplified code

Source: Internet
Author: User

In iOS development, the dictionary-to-model is generally as follows:

1 /**2 * Declaration Method3  */4-(Instancetype) Initwithdictionary: (Nsdictionary *) dict;5+ (Instancetype) carwithdictionary: (Nsdictionary *) dict;6 7 /**8 * Implementation Method9  */Ten-(Instancetype) Initwithdictionary: (Nsdictionary *) Dict One { A     if(self =[Super Init]) { - [self setvaluesforkeyswithdictionary:dict]; -     } the     returnSelf ; - } -  -+ (Instancetype) carwithdictionary: (Nsdictionary *) Dict + { -     return[[Zycar alloc]initwithdictionary:dict]; +}

Declared in the header file before the. m

File to implement specific methods, and the content of these methods are basically consistent, can be simplified with macros.

2. Create a new. h file and add the following code to it:

1 #defineInith (name)2-(Instancetype) Initwithdict: (Nsdictionary *) Dict; 3+ (instancetype) name# #WithDict:(nsdictionary *) dict;4 5 #defineINITM (name)6-(Instancetype) Initwithdict: (Nsdictionary *) Dict7 { 8     if(self =[Super Init]) { 9 [self setvaluesforkeyswithdictionary:dict];Ten     }  One     returnSelf ; A }  -+ (instancetype) name# #WithDict:(nsdictionary *) Dict - {  the     return[[Self alloc] initwithdict:dict]; - } -  - #endif

After quoting this. h file, the Declaration and implementation of the above method can be reduced to a sentence

Inith (name); Initm (name); Name is the model class name

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.