gpt4 book ai didi

ios - 将 NSDictionary(带有自定义类)保存到 NSUserDefaults

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:46 25 4
gpt4 key购买 nike

<分区>

我正在尝试将 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 个字节,与加载它时相同,但它仍然没有填满字典。所以我很茫然。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com