Singletons in objective-C

Source: Internet
Author: User
Background

Singletons classes are an important concept to understand because they exhibit an extremtely useful design pattern.

This idea is used throughout the iPhone SDK, for example, uiapplication has a method called sharedapplication which when called from anywhere will return the uiapplication instance which relates to the currently running application.

How to Implement

You can implement a singleton class in objective-C using the following code:

Mymanager. h

1234567891011
#import <foundation/Foundation.h>@interface MyManager : NSObject {    NSString *someProperty;}@property (nonatomic, retain) NSString *someProperty;+ (id)sharedManager;@end

Mymanager. m

1234567891011121314151617181920212223242526272829
#import "MyManager.h"@implementation MyManager@synthesize someProperty;#pragma mark Singleton Methods+ (id)sharedManager {    static MyManager *sharedMyManager = nil;    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        sharedMyManager = [[self alloc] init];    });    return sharedMyManager;}- (id)init {  if (self = [super init]) {      someProperty = [[NSString alloc] initWithString:@"Default Property Value"];  }  return self;}- (void)dealloc {  // Should never be called, but just here for clarity really.}@end

What this does is it defines a static variable (but only global to this translation unit) calledsharedMyManagerWhich is then initialised once and only once insharedManager. The way we ensure that it's only created once is by usingdispatch_onceMethod from Grand Central Dispatch (GCD). This is thread safe and handled entirely by the OS for you so that you don't have to worry about it at all.

However, if you wowould rather not use GCD then you shoshould use the following codesharedManager:

Non-GCD based code

1234567
+ (id)sharedManager {    @synchronized(self) {        if (sharedMyManager == nil)            sharedMyManager = [[self alloc] init];    }    return sharedMyManager;}

Then you can reference the singleton from anywhere by calling the following function:

MyManager *sharedManager = [MyManager sharedManager];

I 've used this extensively throughout my code for things such as creating a singleton to handle corelocation or coredata functions.

Non-arc Code

Not that I recommend it, but if you are not using automatic reference counting (ARC), then you shoshould use the following code:

Mymanager. H non-Arc

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
#import "MyManager.h"static MyManager *sharedMyManager = nil;@implementation MyManager@synthesize someProperty;#pragma mark Singleton Methods+ (id)sharedManager {  @synchronized(self) {      if(sharedMyManager == nil)          sharedMyManager = [[super allocWithZone:NULL] init];  }  return sharedMyManager;}+ (id)allocWithZone:(NSZone *)zone {  return [[self sharedManager] retain];}- (id)copyWithZone:(NSZone *)zone {  return self;}- (id)retain {  return self;}- (unsigned)retainCount {  return UINT_MAX; //denotes an object that cannot be released}- (oneway void)release {  // never release}- (id)autorelease {  return self;}- (id)init {  if (self = [super init]) {      someProperty = [[NSString alloc] initWithString:@"Default Property Value"];  }  return self;}- (void)dealloc {  // Should never be called, but just here for clarity really.  [someProperty release];  [super dealloc];}@end

1) How do I implement an objective-C Singleton that is compatible with arc?

2) singletons in objective-C

3) iOS | how-to share data between view controllers using a singleton

 

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.