Thursday, April 2, 2015

1)Add Class which inherits NSObject
2)Implement following in YourSingleton.h
                    
#import <foundation/Foundation.h>

@interface YourSingleton: NSObject {
    NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedInstance;

@end
 
 
3)In YourSingleton.m
 
#import "YourSingleton.h"

@implementation YourSingleton

@synthesize someProperty;

#pragma mark Singleton Methods

+ (id)sharedInstance{
    static YourSingleton*sharedInstance= nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance= [[self alloc] init];
    });
    return sharedInstance;
}

- (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
 
 
 
Its Done.....Now you can use it whenever you want. :)
 
 
 
Note : Do not user for creating UIkitcomponent because reference of instance of sigleton are never released.... 

No comments:

Post a Comment