Single-Instance header files for iOS development

Source: Internet
Author: User

//helps implement a single-case design pattern//implementation of the. h file#defineSingletonh (MethodName) + (instancetype) shared# #methodName;//implementation of the. m file#if__has_feature (OBJC_ARC)//It's Arc .#defineSingletonm (MethodName)Static ID_instace =Nil;+ (ID) Allocwithzone: (struct_nszone *) Zone {if(_instace = =Nil) { Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone];}); } return_instace;} - (ID) init {Staticdispatch_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 arc .#defineSingletonm (MethodName)Static ID_instace =Nil;+ (ID) Allocwithzone: (struct_nszone *) Zone {if(_instace = =Nil) { Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone];}); } return_instace;} - (ID) init {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super init];}); return_instace;} +(instancetype) shared# #methodName {return[[Self alloc] init];} -(OneWayvoid) Release {}- (ID) Retain {returnSelf ;} -(Nsuinteger) retaincount {return 1; } + (ID) Copywithzone: (struct_nszone *) Zone {return_instace;} + (ID) Mutablecopywithzone: (struct_nszone *) Zone {return_instace;}#endif

Put the above code into the. h header File

How to use: Implement Singletonh (Mothodname) (MethodName as method name) in a single file,. h

Singletonm implemented in. m (mothodname)

Single-Instance header files for iOS development

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.