【问题标题】:Saving NSDictionary (with custom classes) to NSUserDefaults [duplicate]将 NSDictionary(带有自定义类)保存到 NSUserDefaults [重复]
【发布时间】:2013-11-26 02:48:37
【问题描述】:

我正在尝试将 NSDictionary 保存到我的 NSUserDefualts。

字典由 3 个不同的自定义类组成。

@interface PastOrder : NSObject <NSCoding>
{
    NSDate *timeIn;
    NSDate *timeOut;
    NSString *status;
    NSMutableArray *myItems;
}

@property (nonatomic, retain) NSDate *timeIn;
@property (nonatomic, retain) NSDate *timeOut;
@property (nonatomic, retain) NSString *status;
@property (nonatomic, retain) NSMutableArray *myItems;

@end
@implementation PastOrder

@synthesize timeIn, timeOut, status, myItems;
#define PastOrderTimeInKey @"PastOrderTimeInKey"
#define PastOrderTimeOutKey @"PastOrderTimeOutKey"
#define PastOrderStatusKey @"PastOrderStatusKey"
#define PastOrderMyItemsKey @"PastOrderMyItemsKey"

-(id)initWithCoder:(NSCoder*)decoder
{
    self = [super init];
    if(self)
    {
        self.timeIn = [decoder decodeObjectForKey:PastOrderTimeInKey];
        self.timeOut = [decoder decodeObjectForKey:PastOrderTimeOutKey];
        self.status = [decoder decodeObjectForKey:PastOrderStatusKey];
        self.myItems = [decoder decodeObjectForKey:PastOrderMyItemsKey];
    }
    return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder
{
    [encoder encodeObject:self.timeIn forKey:PastOrderTimeInKey];
    [encoder encodeObject:self.timeOut forKey:PastOrderTimeOutKey];
    [encoder encodeObject:self.status forKey:PastOrderStatusKey];
    [encoder encodeObject:self.myItems forKey:PastOrderMyItemsKey];
}
-(void)dealloc
{
    self.timeIn = nil;
    self.timeOut = nil;
    self.status = nil;
    self.myItems = nil;
}
@end

@interface PastOrderItem : NSObject <NSCoding>
{
    NSNumber *itemID;
    NSString *status;
    NSMutableArray *itemChoices;
}
@property (nonatomic, retain) NSNumber *itemID;
@property (nonatomic, retain) NSString *status;
@property (nonatomic, retain) NSMutableArray *itemChoices;
@end
@implementation PastOrderItem

@synthesize itemID,status,itemChoices;
#define PastOrderItemItemIDKey @"PastOrderItemItemIDKey"
#define PastOrderItemStatusKey @"PastOrderItemStatusKey"
#define PastOrderItemItemChoicesKey @"PastOrderItemItemChoicesKey"
-(id)initWithCoder:(NSCoder*)decoder
{
    self = [super init];
    if(self)
    {
        self.itemID = [decoder decodeObjectForKey:PastOrderItemItemIDKey];
        self.itemChoices = [decoder decodeObjectForKey:PastOrderItemItemChoicesKey];
        self.status = [decoder decodeObjectForKey:PastOrderItemStatusKey];
     }
    return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder
{
    [encoder encodeObject:self.itemID forKey:PastOrderItemItemIDKey];
    [encoder encodeObject:self.itemChoices forKey:PastOrderItemItemChoicesKey];
    [encoder encodeObject:self.status forKey:PastOrderItemStatusKey];
}
-(void)dealloc
{
    self.itemID = nil;
    self.itemChoices = nil;
    self.status = nil;
}
@end

@interface PastOrderItemChoice : NSObject <NSCoding>
{
    NSNumber *modifierID;
    NSNumber *modifierChoice;
}
@property (nonatomic, retain) NSNumber *modifierID;
@property (nonatomic, retain) NSNumber *modifierChoice;
@end
@implementation PastOrderItemChoice

@synthesize modifierID, modifierChoice;
#define PastOrderItemChoiceModifierIDKey @"PastOrderItemChoiceModifierIDKey"
#define PastOrderItemChoiceModifierChoiceKey @"PastOrderItemChoiceModifierChoiceKey"
-(id)initWithCoder:(NSCoder*)decoder
{
    self = [super init];
    if(self)
    {
        self.modifierID = [decoder decodeObjectForKey:PastOrderItemChoiceModifierIDKey];
        self.modifierChoice = [decoder decodeObjectForKey:PastOrderItemChoiceModifierChoiceKey];
     }
    return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder
{
    [encoder encodeObject:self.modifierID forKey:PastOrderItemChoiceModifierIDKey];
    [encoder encodeObject:self.modifierChoice forKey:PastOrderItemChoiceModifierChoiceKey];
}
-(void)dealloc
{
    self.modifierID = nil;
    self.modifierChoice = nil;

}
@end

这三个类将在这个 NSDictionary 中。 这是我加载和保存它的方法。

-(void)SavePrefs
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData* data=[NSKeyedArchiver archivedDataWithRootObject:self.myDictionary];
    [prefs setObject:data forKey:@"SavedOrders"];
    [prefs synchronize];
}
- (id)init
{
    self = [super init];
    if (self)
    {
        NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"SavedOrders"];
        self.myDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];

      }
    return self;
}

我已经对代码进行了一些试验,到目前为止,最好的情况是,当我保存字典时,它是 135 字节,与我加载它时相同,但它仍然没有填满字典。所以我很茫然。

【问题讨论】:

    标签: ios objective-c dictionary nsuserdefaults


    【解决方案1】:

    您的代码似乎不错。我找不到错误所以尝试换行:

    self.myDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    

    id unknownObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"%@",[unknownObject class]);
    

    看看@控制台。如果输出是字典,也许你也应该尝试强制转换。所以尝试将其更改为:

    self.myDictionary = (NSDictionary*)[NSKeyedUnarchiver unarchiveObjectWithData:data];
    

    编辑

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",@"object3",@"key3", nil];
    NSLog(@"before: %@",dictionary);
    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
    NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    NSLog(@"after: %@",myDictionary);
    

    输出:

    2013-11-13 14:32:31.369 DemoM[175:60b] before: {
        key1 = object1;
        key2 = object2;
        key3 = object3;
    }
    2013-11-13 14:32:31.372 DemoM[175:60b] after: {
        key1 = object1;
        key2 = object2;
        key3 = object3;
    }
    

    【讨论】:

    • 当你归档时,我认为你必须使用编码器进行编码,并在所有正在编码/解码的对象中使用编码器初始化方法实现 init
    • 我不这么认为..我只是写了一段示例代码,并且工作正常。
    • 好吧,如果它工作它工作:D
    • 你的第一个例子不起作用,但第二个例子起作用了。但是当我将其更改为使用我保存的数据时,它返回一个空字典。
    • 您正在处理的数据字典显示的数据类型为 364 字节,已经是我保存的字典大小的两倍多。这不可能是对的,所以字典一定是存错了。如果我在自定义类数组中有自定义类,我也不需要将它们更改为数据吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多