List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Friday, May 12, 2017

iOS Singleton Class


To set values, use the below code.

YourSingletonClass *sharedManager = [YourtSingletonClass sharedManager];
sharedManager.nameString =@"name";
sharedManager.emailString =@"name@domain.com";

For getting values, use the below code
YourSingletonClass *sharedManager = [YourtSingletonClass sharedManager];
NSString *nameVal=sharedManager.nameString;
NSString *emailVal=sharedManager.emailString;


/////YourSingletonClass.h class

#import

@interface YourSingletonClass : NSObject{
    NSString *nameString;
    NSString *emailString;
}

@property (nonatomic, strong) NSString *nameString;
@property (nonatomic, strong) NSString *emailString;
+ (id)sharedManager;
@end

/////YourSingletonClass.m class

#import "YourSingletonClass.h"
@implementation YourSingletonClass
@synthesize nameString;
@synthesize emailString;

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

- (id)init {
    if (self = [super init]) {
        self.nameString = @"";
        self.emailString = @"";
    }
    return self;
}

- (void)dealloc {
    // Should never be called, but just here for clarity really.
}
@end



No comments:

Post a Comment