iOS single-case macro

Source: Internet
Author: User

First of all, a simple mention of a single-column concept, of course, the specific description may be benevolent see!
1. Single case design mode (Singleton)
1> What is a single column: it guarantees that the object created by a class will always be only 1

2> effect (why use)

    • Save Memory overhead
    • If there is some data, the whole program is used, only the same resources (to ensure that the data you access are the same, consistent)
    • In general, the tool design is more appropriate for a single case model

3> How to achieve, the old programmer is encountering such a problem! There's less under arc!

    • MRC (non-ARC)
    • ARC

Cut the crap, take a look at my header file in single-case mode! Here the main is __has_feature (OBJC_ARC) judged whether it is arc, so the code looks a bit more!

Helps implement a single-case design pattern

Implementation of the. h file

Define SINGLETONH (MethodName) + (instancetype) shared# #methodName;

Implementation of the. m file

If __has_feature (OBJC_ARC)//Is arcDefine SINGLETONM (MethodName) \

Static id _instace = NIL; \

    • (ID) Allocwithzone: (struct nszone *) zone \
      { \
      if (
      instace = = nil) {\
      Static dispatch_once_t Oncetoken; \
      Dispatch_once (&oncetoken, ^{\
      Instace = [Super Allocwithzone:zone]; \
      }); \
      } \
      return
      Instace; \
      } \
      \
    • (ID) init \
      { \
      Static dispatch_once_t Oncetoken; \
      Dispatch_once (&oncetoken, ^{\
      Instace = [Super init]; \
      }); \
      return
      Instace; \
      } \
      \
    • (instancetype) shared# #methodName \
      { \
      return [[Self alloc] init]; \
      } \
    • (ID) Copywithzone: (struct nszone *) zone \
      { \
      return
      Instace; \
      } \
      \
    • (ID) Mutablecopywithzone: (struct nszone *) zone \
      { \
      return
      Instace; \
      }
else//Not arcDefine SINGLETONM (MethodName) \

Static id _instace = NIL; \

    • (ID) allocwithzone: (struct nszone *) zone \
      {\
      if (
      instace = = nil) {\
      static dispatch_once _t Oncetoken; \
      Dispatch_once (&oncetoken, ^{\
      instace = [Super Allocwithzone:zone]; \
      }); \
      } \
      Return
      instace; \
      } \
      \
    • (ID) init \
      {\
      static dispatch_once_t oncetoken; \
      Dispatch_once (&oncetoken, ^{ \
      instace = [super init]; \
      }; \
      return
      instace; \
      } \
      \
    • (instancetype) shared# #m Ethodname \
      {\
      return [[Self alloc] init]; \
      } \
      \
    • (oneway void) release \
      {\
      \
      } \
      \
    • (ID) retain \
      {\
      return self; \
      } \
      \
    • (nsuinteger) retaincount \
      {\
      return 1; \
      } \
    • (ID) copywithzone: (struct nszone *) zone \
      {\
      return
      instace; \
      } \
      \
    • (ID) mutablecopywithzone: (struct nszone *) zone \
      {\
      return
      instace; \
      }
endif

This is completely done, single-column all considerations, including copy case, multi-threading, and automatic judging arc and MRC situation!
Use only when you include the header file and then use the following
. h file: Singletonh (Httptool)
. m file: Singletonm (Httptool)

This thing who use who know!!!! Hee Hee Hope to be helpful to the reader!

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.