Question

I have a singleton and I pass data to it but it returns null can you please help me in my situation. Thanks in advance :)

Here's my code

Card.h

@property (weak,nonatomic) NSString *email;
@property (weak,nonatomic) NSString *fName;
@property (weak,nonatomic) NSString *lName;
@property (weak,nonatomic) NSString *category;

+(Card *)getCard;

Card.m

@synthesize email;
@synthesize fName;
@synthesize lName;
@synthesize category;

static csCard *instance;

+(Card *) getCard
{
    @synchronized (self)
    {
        if(instance == nil)
        {
            instance = [[Card alloc]init];
        }
    }
    return instance;
}

- (id) init{

    self.email = [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
    self.fName = [[NSUserDefaults standardUserDefaults]stringForKey:@"firstName"];
    self.lName = [[NSUserDefaults standardUserDefaults]stringForKey:@"lastName"];
    self.category = @"TestCategory";

    return self;
}

and here's my test code to see if it's working

Test.m

Card *card = [Card getCard];
    [card setEmail:self.emailField.text];
    NSLog(@"%@",card.email);

but this code give me (null)

Was it helpful?

Solution

Modify your class like this.

Card.h

@property (strong,nonatomic) NSString *email; //Let the modal be strong property
@property (strong,nonatomic) NSString *fName;
@property (strong,nonatomic) NSString *lName;
@property (strong,nonatomic) NSString *category;

+(Card *)getCard;

Card.m

static Card *instance;

+(Card *) getCard
{
    @synchronized (self)
    {
        if(instance == nil)
        {
            instance = [[Card alloc]init];
        }
    }
    return instance;
}
- (NSString)email{
return [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
}

- (void)setEmail:(NSString)email{
[[NSUserDefaults standardUserDefaults] setString:email forkey:@"email"];
}

No need of overriding init

in your test class

Card *card = [Card getCard];
    [card setEmail:self.emailField.text];
    NSLog(@"%@",card.email);

OTHER TIPS

static csCard *instance;

+(csCard *) getCard
{
@synchronized (self)
{
    if(instance == nil)
    {
        instance = [[csCard alloc]init];
    }
}
return instance;
}

Replace it with this code

static Card *instance;

 +(Card *) getCard
{
@synchronized (self)
{
    if(instance == nil)
    {
        instance = [[Card alloc]init];
    }
}
return instance;
}

The Class name Of the instance Object was wrong and In singleton method,the return datatype was also wrong. I think u will understand what I am saying.

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

It should be work

With the help of what βhargavḯ sujjested u can modify your code as below because in the line static csCard *instance; u are using csCard i think it is typo so better u can do like this,


  #import "Card.h"
  static dispatch_once_t onceDispach;

  @implementation Card
  @synthesize email = _email;
  @synthesize fName;
  @synthesize lName;
  @synthesize category;

   static Card *instance = nil; //change this to Card because this instance which is of type Card

 +(Card *)getCard
  {
     dispatch_once(&onceDispach, ^{
     instance = [[self alloc] init];//careate Card shared object named instance
      });
    return instance;
  }

  - (id) init
  {

      self.email = [[NSUserDefaults standardUserDefaults]stringForKey:@"email"];
      self.fName = [[NSUserDefaults standardUserDefaults]stringForKey:@"firstName"];
      self.lName = [[NSUserDefaults standardUserDefaults]stringForKey:@"lastName"];
      self.category = @"TestCategory";

      return self;
  }
  @end

 - (NSString *)email
   {
      return _email;
   }

 - (void)setEmail:(NSString *)email
  {
    _email = email;
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
   [userDefault setObject:email forKey:@"email"];
  }


in the class where u are using this shared instance use like below

  - (void)actionMathodCalled
   {
     Card *card = [Card getCard];
     NSLog(@"before saving to defaults->%@",card.email);
     [card setEmail:@"happyCoding@ymail.com"];
     NSLog(@"after savng to defaults->%@",card.email);

   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top