SharedInstanceGCD 1.0.2

SharedInstanceGCD 1.0.2

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.


Downloads

Total4,075
Week0
Month0

Installs

Apps198
Test Targets9
powered by Segment

GitHub

Stars22
Watchers1
Forks2
Issues0
Contributors1
Pull Requests0

Code

Files1
LOCLines of Code 41


  • By
  • Tim Shadel

A collection of the best GCD singleton macros.

Cleanup your code!

There are lots of GCD singleton macros out there. Some are from Apple, some are on Github, some are on Stack Overflow. This collects the three main types into one header file, easily included by CocoaPods.

Create Singletons

There are 3 main ways to create singletons using these macros.

Use sharedInstance

This is the best one, in my opinion. It follows a prevailing naming convention in recent Cocoa modules, and needs no parameters.

The first example relies on the convention that the designated initializer for your class is simply init. The second uses a block to allow you to call any initializer.

sharedInstance, default initializer

// MyClass.h

@interface MyClass
+ (instancetype)sharedInstance;
@end

// MyClass.m

@implementation MyClass
SHARED_INSTANCE_CGD
@end

sharedInstance, custom initializer

// MyClass.h

@interface MyClass
+ (instancetype)sharedInstance;
@end

// MyClass.m

@implementation MyClass
SHARED_INSTANCE_GCD_USING_BLOCK(^{
  return [[MyClass alloc] initWithAnotherInitializer];
})
@end

Use sharedClassname

This one follows a longer-standing naming convention in Cocoa modules, and needs to be told what this class is.

sharedClassname, default initializer

// Database.h

@interface Database
+ (instancetype)sharedDatabase;
@end

// Database.m

@implementation Database
SHARED_INSTANCE_CGD_WITH_NAME(Database)
@end

sharedClassname, custom initializer

// Database.h

@interface Database
+ (instancetype)sharedDatabase;
@end

// Database.m

@implementation Database
SHARED_INSTANCE_GCD_WITH_NAME_USING_BLOCK(Database, ^{
  return [[Database alloc] initWithAnotherInitializer];
})
@end

Method Body

This one gives you the most flexibility: you can use any method name. Unlike the other options, you have to define the method in your implementation file.

Method body only approach

// Utility.h

@interface Utility
+ (instancetype)defaultUtility;
@end

// Utility.m

@implementation Utility

+ (instancetype)defaultUtility
{
    DEFINE_SHARED_INSTANCE_GCD_USING_BLOCK(^{
        return [[Utility alloc] initWithAnotherInitializer];
    })
}

@end

Installing the Macros

You can simply copy the header file into your project.

If you want updates, use CocoaPods:

pod 'SharedInstanceGCD'

License

MIT.

I've consolidated the macros written by several people. If you'd like to look at a couple, check out this gist, and this SO article.