Breaking News
Loading...
Wednesday, July 31, 2013

IOS Singleton Object

11:27 PM
After long search , i have got the clear definition of iOS Singleton Object class, it nothing and very simple ,it's a like global class or variable  in iOS

so if you used singleton class mean you can access value  globally or data any where or any controller

eg:
 i have set value as first view controller after i can access that value to any another controller. with out passing data between view controller.

advantage:
memory
Easy flow
clear coding

Now i will tell u how to make singleton class

Step 1:
create nsobject class just like below

// File.h

#import <Foundation/Foundation.h>

@interface File : NSObject
{
    NSString *fileName;
}
@property(nonatomic,retain)NSString *fileName;

+ (File *)SharedInstance;
@end

// File.m
#import "File.h"

@implementation File
@synthesize fileName;
static File *file = nil;
+(File *)SharedInstance
{
    @synchronized(self)
    {
        if (file == nil) {
            file = [File new];
        }
    }
    return file;
}
@end

Then you can access value like below

 File *file =([File SharedInstance]);
    file.fileName = @"dhayaladevan";
    
    NSLog(@"%@",file.fileName);

hope i explained well.


0 comments:

 
Toggle Footer