Skip to content

Subclassible, thread-safe ARC singleton for iOS, Objective-C, Cocoa Touch, iPhone, iPad

Notifications You must be signed in to change notification settings

MultX/KLSingleton

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Description

This is a singleton class for iOS, Objective-C, Cocoa Touch, iPhone, iPad.

KLSingleton is:

  1. Used in production
  2. Subclassible (to the n-th degree)
  3. ARC compatible
  4. Safe with alloc and init
  5. Loaded lazily
  6. Thread-safe
  7. Lock-free (uses +initialize, not @synchronize)
  8. Macro-free
  9. Swizzle-free
  10. Simple

This implementation "loads lazily" (which is good). It does not "require explicit initialization" and it does not "lock to allocate". This implementation eliminates the iOS singleton boilerplate.

Use

  1. Add the files to your project
  2. Import the header using #import "KLSingleton.h"
  3. Subclass the KLSingleton class in the following way:
@interface MYSubclass : KLSingleton

You may then retrieve the unique, ready-to-use instance of your class by calling any of the following methods:

[MYSubclass instance]
[MYSubclass sharedInstance] //alias
[MYSubclass singleton]      //alias
[[MYSubclass alloc] init]   //bad style, but safe to call any number of times

License

Released under ISC (similar to 2-clause BSD)

About

Subclassible, thread-safe ARC singleton for iOS, Objective-C, Cocoa Touch, iPhone, iPad

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 54.6%
  • Ruby 45.4%