无法从NSUserDefaults读取自定义对象数组

时间:2014-05-12 07:06:17

标签: ios objective-c arrays nsuserdefaults

所以我对singleton数组有一个custom objects。我为自定义对象的类编写了一个编码和解码器,它似乎正确存储似乎我的意思是传递一个non nil array个对象来存储。我不知道如何查看该数组在内存中是否为非零。但是当我尝试在内存中读取这个数组时,它会读出为零。

这是默认值的初始值设定项

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"];

我将9个对象写入默认存储区,当我设置断点并查看它时,它们都是非零值

这里是编码器和解码器

- (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;
}

这是我试图填补单身人士的地方

- (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;
}

变量oldArray是我放入的正确大小,但所有属性都是零 savedArray的大小为1438 bytes我不知道这是否适合我写的内容。 任何帮助将不胜感激谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这种方式来制作单身人士的正确方法:

让课程让我们说AppManager

在AppManager.h文件中:

#import <Foundation/Foundation.h>

@interface AppManager : NSObject

@property (strong, nonatomic) NSMutableArray *privateItems;

+ (instancetype) sharedInstance;

@end
AppManager.m文件中的

#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
初始化程序中的

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");

如果您想调用您的privateItems内容,请执行以下操作:

AppManager *appManager = [AppManager sharedInstance];

NSArray *array = [appManager privateItems];

这就是它,这是制作单身人士的正确方法;)

随时提供帮助和理解。

* 不要伪造导入AppManager.h文件,我的建议是将其导入您的&#34;项目名称&#34; -Prefix.pch文件以使其可用对于您的所有应用范围,单身人士的想法使其在整个APP上都可用。