Objective-C combines GCD Singleton mode and macro Template under ARC

Source: Internet
Author: User

The Singleton mode is often used in iOS development. Apple has provided the following official methods to use objective c singleton:

static MyGizmoClass *sharedGizmoManager = nil; + (MyGizmoClass*)sharedManager{    @synchronized(self) {        if (sharedGizmoManager == nil) {            [[self alloc] init]; // assignment not done here        }    }    return sharedGizmoManager;} + (id)allocWithZone:(NSZone *)zone{    @synchronized(self) {        if (sharedGizmoManager == nil) {            sharedGizmoManager = [super allocWithZone:zone];            return sharedGizmoManager;  // assignment and return on first allocation        }    }    return nil; //on subsequent allocation attempts return nil} - (id)copyWithZone:(NSZone *)zone{    return self;} - (id)retain{    return self;} - (unsigned)retainCount{    return UINT_MAX;  //denotes an object that cannot be released} - (void)release{    //do nothing} - (id)autorelease{    return self;}

Now, the ARC technology is widely used after iOS5. It turns out that this method is cumbersome and release is no longer used.

We can use GCD to implement the singleton mode:

+ (id)sharedInstance{  static dispatch_once_t pred = 0;  __strong static id _sharedObject = nil;  dispatch_once(&pred, ^{    _sharedObject = [[self alloc] init]; // or some other init method  });  return _sharedObject;}

When dispatch_once is used, this method means that during the application lifecycle, this method is only executed once. This is the code of a singleton combined with GCD under ARC.

Because we may need multiple Singleton classes, it is troublesome to write them once, which makes it easier to write the code into a macro, you only need to input a class name to create a singleton class.

Create the name of the singleton method in the header file by passing in the class name:

#define DEFINE_SINGLETON_FOR_HEADER(className) \\+ (className *)shared##className;

How to Create a singleton by passing in a class name

#define DEFINE_SINGLETON_FOR_CLASS(className) \\+ (className *)shared##className { \static className *shared##className = nil; \static dispatch_once_t onceToken; \dispatch_once(&onceToken, ^{ \shared##className = [[self alloc] init]; \}); \return shared##className; \}

Use macros:

A new class testSingleton is created in the project, and there are. h and. m files.

. H

Code in. m:

#import "testSingleton.h"@implementation testSingletonDEFINE_SINGLETON_FOR_CLASS(testSingleton)@end

#define DEFINE_SINGLETON_FOR_HEADER(className) \\+ (className *)shared##className;#define DEFINE_SINGLETON_FOR_CLASS(className) \\+ (className *)shared##className { \static className *shared##className = nil; \static dispatch_once_t onceToken; \dispatch_once(&onceToken, ^{ \shared##className = [[self alloc] init]; \}); \return shared##className; \}#import <Foundation/Foundation.h>@interface testSingleton : NSObjectDEFINE_SINGLETON_FOR_HEADER(testSingleton);@end

You can put the macro in the project management macro file. This is to facilitate the demonstration and put it in the testSingleton. h file.
Obtain a ticket:

- (void)test{    testSingleton *testShare = [testSingleton sharedtestSingleton];}

If you have any questions, leave a message.

Rong Fangzhi (http://blog.csdn.net/totogo2010)

This article follows the "signature-non-commercial use-consistency" creation public agreement

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