iOS uses a macro to write a single example

Source: Internet
Author: User

This article only describes the single case of Arc

The past has not been back down how to write a single case, that is, know this, but also know through the macro to write a single case, but can not remember, today to record

-(void) viewdidload {[Super viewdidload];    Siperson *person = [[Siperson alloc] init];    NSLog (@ "%@", person);    Siperson *person1 = [[Siperson alloc] init]; NSLog (@ "%@", Person1);}

Create person, print, actually 2 objects. No problem.

Create a method

 #import   "SIPerson.h" static siperson *instance_ = nil;@ implementation siperson///Method 1, quickly create object +  (instancetype) sharedinstance{    static  dispatch_once_t oncetoken;    dispatch_once (&onceToken, ^{         instance_ = [[self alloc] init];     });     return instance_;} Method 2. This method must have, that is alloc] init] method, will be called this method +  (instancetype) Allocwithzone: (struct _nszone *) Zone{    static dispatch_once_t oncetoken;    dispatch_once ( &onceToken, ^{        instance_ = [super  allocwithzone:zone];    });     return instance_;} @end//There should also be a + copy method, because it might be copy, then it is possible to generate a new method 

Method 12 must be implemented in order to be a singleton. If Method 2 is not implemented, sharedInstance the implementation is really a singleton, but by alloc] init] having another object generated

2016-09-17 14:17:45.086 sharedinstance[9158:611161] 2016-09-17 14:17:45.087 sharedinstance[9158:611161]

If your object is likely to be called in the future copy (the protocol should be declared), you should also implement a method

-(ID) Copywithzone: (Nszone *) zone{return instance_;}

Copy of the time, is generally generated a new object, so it is not a single case, but the use of less, not particularly needed, can not implement this method, for Mao to write this way? Because he's the object method, instance_ there's a value inside.

[Super Viewdidload];    Siperson *person = [Siperson sharedinstance];    NSLog (@ "%@", person);    Siperson *person1 = [[Siperson alloc] init];        NSLog (@ "%@", Person1);    Siperson *person2 = [Person1 copy]; NSLog (@ "%@", Person2);

The results are as follows

2016-09-17 14:24:10.555 sharedinstance[9199:615987] 2016-09-17 14:24:10.555 sharedinstance[9199:615987] 2016-09-17 14:24:10.556 sharedinstance[9199:615987]

/** *   macros defined in the. h file, Arc * *  sisingletonh (name)   This is a macro  *  +   (instancetype) shared# #name; This is a substituted method,  # #代表着shared +name  highly customizable  *  outside we use   " Sisingletonh (Gege)   then in the. h file, a method "+  (instancetype) sharedgege" is defined, so the first letter is capitalized  * *    @return   A well-done method name  */#define  SISINGLETONH (name)  +  (instancetype) shared# #name;/**  *   macro  arc * *  sisingletonm (name) in. m files   This is a macro, because it's a multi-line thing, So there is a "\" behind each line, except for the last line, *  to pass a "name" because there is a way to name "+  (instancetype) shared# #name"  *    @return   Simple Interest  */#define  SISINGLETONM (name)  static siperson *instance_ =  nil;+  (instancetype) shared# #name {    static dispatch_once_t oncetoken ;     dispatch_once (&onceToken, ^{         Instance_ = [[self alloc] init];    });     return instance_;} +  (Instancetype) Allocwithzone: (struct _nszone *) zone{    static  Dispatch_once_t oncetoken;    dispatch_once (&onceToken, ^{         instance_ = [super allocwithzone:zone];    }) ;     return instance_;} -  (ID) Copywithzone: (nszone *) Zone{    return instance_;}

Actual use

. h file Sisingletonh (Default)
. m file Sisingletonm (Default)

is a sentence, there is no symbol (when the definition of the symbol), it is so simple

In the actual use of the time

[Super Viewdidload];    Siperson *person = [Siperson Shareddefault];    NSLog (@ "%@", person);    Siperson *person1 = [[Siperson alloc] init];    NSLog (@ "%@", Person1);    Siperson *person2 = [Person1 copy]; NSLog (@ "%@", person2);//Print results 2016-09-17 14:56:39.508 sharedinstance[9292:633076] 2016-09-17 14:56:39.508 SHAREDINSTANCE[9292:633076] 2016-09-17 14:56:39.508 sharedinstance[9292:633076]

The file address you can use to get it.

Simply say how to define a swift version of a single case, normal writing, no study of single cases

iOS uses a macro to write a single example

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: 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.