IOS-----Write a canonical list of--->

Source: Internet
Author: User

1. Integration of a macro

2. Two sentence Code integration single case

3. One line of code calls a single case

------------->

1. Integration of a macro

Here to pay attention, because each singleton, the method name can be different, then we can't write the name to die, to flexible customization, then the call to him Bogwang Ah, then!!! Note that the join symbol # #的使用, the passed in parameters should be placed after the connection symbol as follows

#define IVANSINGLETONH (name) + (instancetype) shared# #name;

Note here, the general macro will only judge the name of the macro followed by the paragraph is its code, so add \ symbol to tell him that the next line is also the code he manages

#pragma note that this type is written as an ID type because the singleton it receives is of different types

#define IVANSINGLETONM (name) \

Static ID _# #name; \

\

+ (Instancetype) Allocwithzone: (struct _nszone *) zone\

{\

Static dispatch_once_t oncetoken;\

Dispatch_once (&oncetoken, ^{\

_# #name = [Super allocwithzone:zone];\

});\

return _# #name; \

}\

+ (instancetype) shared# #name \

{\

Static dispatch_once_t oncetoken;\

Dispatch_once (&oncetoken, ^{\

_# #name = [[Self alloc]init];\

});\

return _# #name; \

}\

-(ID) Copywithzone: (Nszone *) zone\

{\

return _# #name; \

}

2. Two sentence Code integration single case

Write a code for the. h file of the coded area of the single example:

Ivansingletonh (name), name write the singleton method name that you actually want to invoke

Write a code in the corresponding. m file:

IVANSINGLETONM (name), name write the method name of the singleton that you actually want to invoke (and the name of the. h file above is the same)

3. One line of code calls a single case

In the case where you want to invoke the singleton, import the header file of the singleton:

#import "ClassName.h"

Calling code:

[ClassName ShareName];

IOS-----Write a canonical list of--->

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.