Question

so I have a singleton for an array of custom objects. I wrote a encode and decoder for the class the custom object and it seems to store properly by seems I mean a pass it a non nil array of objects to store. I am not sure on how to see if that array is non nil in memory though. But when I try to read this array in memory it reads out as nil.

here is the initializers for the defaults

BYFFeedItem *item9 = [[BYFFeedItem alloc] init];
             item9.name =@"meal solv 44";
             item9.netEnergyM = @"2.06";
             item9.netEnergyG = @"1.40";
             item9.crudePro = @"49.9";
             item9.calcium =@"0.33";
             item9.phosphorus=@".71";
             [defaultStore addObject:item9];

             NSLog(@"init the custom defualts");
             [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:defaultStore] forKey:@"itemStore"];

i wrote 9 objects to default store and when i put a break point and look at it they are all non nil values

here is the encoder and decoder

- (void)encodeWithCoder:(NSCoder *)encoder {
    //[super encodeWithCoder: encoder]; // not in this case
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeObject:self.amount forKey:@"amount"];
    [encoder encodeObject:self.netEnergyM forKey:@"netEnergyM"];
    [encoder encodeObject:self.netEnergyG forKey:@"NetEnergyG"];
    [encoder encodeObject:self.crudePro forKey:@"crudePro"];
    [encoder encodeObject:self.calcium forKey:@"calcium"];
    [encoder encodeObject:self.phosphorus forKey:@"phosphorus"];
    [encoder encodeObject:self.cost forKey:@"cost"];
    // [encoder encodeObject:self.sets forKey:@"sets"];

}
- (id) initWithCoder:(NSCoder *)decoder {
    //self = [super initWithCoder: decoder]; // not in this case
    self = [super init];
    self->_name = [decoder decodeObjectForKey:@"name"];
    self->_amount = [decoder decodeObjectForKey:@"amount"];
    self->_netEnergyM = [decoder decodeObjectForKey:@"netEnergyM"];
    self->_netEnergyG = [decoder decodeObjectForKey:@"netEnergyG"];
    self->_crudePro = [decoder decodeObjectForKey:@"crudePro"];
    self->_calcium = [decoder decodeObjectForKey:@"calcium"];
    self->_phosphorus = [decoder decodeObjectForKey:@"phosphorus"];
    self->_cost = [decoder decodeObjectForKey:@"cost"];

    // self->_sets = [decoder decodeObjectForKey:@"sets"];


    return self;
}

and here is where i am trying to fill the singleton

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
        NSData *savedArray = [currentDefaults objectForKey:@"itemStore"];
        if (savedArray != nil)
        {
            NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:savedArray];
            if (oldArray != nil) {
                self.privateItems= [[NSMutableArray alloc] initWithArray:oldArray];
            } else {
                self.privateItems= [[NSMutableArray alloc] init];
            }
        }
    }
    return self;
}

the variable oldArray is the right size of what I put in but all the properties are nil savedArray is of the size 1438 bytes I don't know if that is the right size for what i wrote in or not. any help would be appreciated Thanks!

Was it helpful?

Solution

Try this way for correct way to make singleton:

make class let's say AppManager

In the AppManager.h file:

#import <Foundation/Foundation.h>

@interface AppManager : NSObject

@property (strong, nonatomic) NSMutableArray *privateItems;

+ (instancetype) sharedInstance;

@end

in the AppManager.m File:

#import "AppManager.h"

@implementation AppManager
@synthesize privateItems;

static id sharedInstance;

+ (instancetype) sharedInstance{

   @synchronized(self){
        if(sharedInstance == nil)
        {
            sharedInstance = [[self alloc]init];
        }
   }  
   return sharedInstance;
}  

+ (void)load{
   [super load];
   [self sharedInstance];
}


- (id)init
  {
   self = [super init];
   if (self) {        
       self.privateItems = [[NSMutableArray alloc]init];                                        
       }
   }

   return self;
}
@end

in your initializer:

BYFFeedItem *item9 = [[BYFFeedItem alloc] init];
         item9.name =@"meal solv 44";
         item9.netEnergyM = @"2.06";
         item9.netEnergyG = @"1.40";
         item9.crudePro = @"49.9";
         item9.calcium =@"0.33";
         item9.phosphorus=@".71";

AppManager *appManager = [AppManager sharedInstance];
[[appManager privateItems] addObject:item9];


NSLog(@"init the custom defualts");

When you want to call your privateItems content just make:

AppManager *appManager = [AppManager sharedInstance];

NSArray *array = [appManager privateItems];

That's it, this is the right way to make singleton ;)

Hop it's helping and understood enough.

* Don't forger to import AppManager.h file, my recommendation is to import it to your "project name"-Prefix.pch file for make it available to all your app scope, that's the idea of singleton to make it available all over the APP.

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