2

I have a singleton class which I intend to share throughout my whole application. It looks like this:

.m

    @implementation PersonalGlobal

@synthesize firstName;
@synthesize lastName;
@synthesize SSN;
@synthesize customerNo;
@synthesize email;
@synthesize address;
@synthesize city;
@synthesize postalCode;
@synthesize telNo;
@synthesize mobileNo;

#pragma mark Singleton Methods

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


- (void)dealloc {
    [super dealloc];
    // Should never be called
}

@end

.h

#import <foundation/Foundation.h>

@interface PersonalGlobal : NSObject {
    NSString *firstName;
    NSString *lastName;
    NSString *SSN;
    NSString *customerNo;
    NSString *email;
    NSString *address;
    NSString *city;
    NSString *postalCode;
    NSString *telNo;
    NSString *mobileNo;    
}

@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;
@property (nonatomic, retain) NSString *SSN;
@property (nonatomic, retain) NSString *customerNo;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *city;
@property (nonatomic, retain) NSString *postalCode;
@property (nonatomic, retain) NSString *telNo;
@property (nonatomic, retain) NSString *mobileNo;

+ (id)sharedPersonal;

@end

In the code I save strings to it like so:

PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
sharedPersonal.firstName = @"Some string";

But when I change view and try to access the string like this:

   PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
   //Setting some textfield    
   sometextfield.text = sharedPersonal.firstName;

I get nothing. I have done a #import "PersonalGlobal.h" in all the files. Can i commit the changes in any way to the singleton class?

Can anyone see what I am doing wrong here? Thanks!

PaperThick
  • 2,749
  • 4
  • 24
  • 42
  • I'd loose the iVars and try again. – Desdenova Apr 12 '13 at 10:14
  • instead of using "id" check with this +(PersonalGlobal *) sharedInstance { static PersonalGlobal *_instance = nil; @synchronized(self) { if(_instance == nil) { _instance = [[super alloc] init]; } } return _instance; } – Madhu Apr 12 '13 at 10:32

2 Answers2

1

Your implementation is looking fine. This should work if your singleton returning the only one instance . So the point of doubt in your sharedPersonal method . Just try to add breakpoints in this method and see whether it is creating a new instance every time .for the reference I got this SO question.

If not then you can also try this :

+(SingleTon *)getSharedInstance
{
    static PersonalGlobal *sharedPersonalGlobal = nil;
    if (sharedPersonalGlobal==nil)
    {
        sharedPersonalGlobal=[[PersonalGlobal alloc]init];
    }
    return sharedPersonalGlobal;
}

This is working code for me.

Community
  • 1
  • 1
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
0

Your singleton implementation looks ok. I wouldn't be surprised if in this case the code that you wrote to set the firstName just never gets executed. If I were you I would step through the codes.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156