gpt4 book ai didi

ios - 使用未在较低级别对象上调用的编码器进行编码

转载 作者:行者123 更新时间:2023-11-28 19:46:51 24 4
gpt4 key购买 nike

我正在更新现有应用以遵循 MVC 设计。我创建了一个顶级数据模型类。在这个 dataModel 类中,我存档和取消存档一个子数组 (self.children)。这是一个具有 items 属性的 Child 对象数组。 items 属性是一个 BabyMilestone 对象数组。 Child 类和 BabyMilestone 类都负责自己进行编码/解码。 Child 类中的对象正在正确编码和解码。 BabyMilestone 类中的对象不是。没有在 BabyMilestone 对象上调用编码器编码,而是在子类上调用。
问题:我是否需要做一些特殊的事情来编码作为其他对象属性的对象数组?欢迎任何调试建议!

enter image description here

处理 .plist 的保存和加载的顶级 DataModel 类

@implementation DataModel
-(id)init
{
if ((self = [super init]))
{
[self loadBabyMilestones];
}
return self;
}

/////////////Loading and Saving Methods////////////////
//Path to documentsDirectory for loading and saving the .plist file of BabyMilestone Objects
-(NSString *)documentsDirectory
{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
return documentsDirectory;
}

-(NSString *)dataFilePath
{ return [[self documentsDirectory] stringByAppendingPathComponent:@"BabyMilestones.plist"];
NSLog(@"The documents directory is %@", [self documentsDirectory]);
}

//Save the BabyMilestoneitems to the .plist
-(void) saveBabyMilestoneItems
{
Child *child;
child = [[Child alloc] init];
child = [self.children objectAtIndex:0 ];
NSLog(@"The length of the children array in the SAVE method is %lu", child.items.count);

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:self.children forKey:@"Children"];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
NSLog(@"In saveBabyMilestoneItems");
}

//Loading the babyMilestones data from the .plist file
-(void) loadBabyMilestones
{
NSString *path = [self dataFilePath];
NSLog(@"Data file path is %@", [self dataFilePath]);
if ([[NSFileManager defaultManager]fileExistsAtPath:path]) //if file exists load self.children
{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
self.children = [unarchiver decodeObjectForKey:@"Children"];
[unarchiver finishDecoding];
NSLog(@"In loadbabymilestones loading from file");
}
//Default list
else {
self.children = [[NSMutableArray alloc]initWithCapacity:2];
Child *child;
child = [[Child alloc] init];
child.name = @"Test Child 1";
[self.children addObject:child];

child = [[Child alloc]init];
child.name = @"Test Child 2";
[self.children addObject:child];
for (Child *child in self.children) {
babyMilestone *babyItem;
babyItem = [[babyMilestone alloc] init];
babyItem.text = @"Arrives Home";
babyItem.backgroundImageName = @"newText_home";
babyItem.imageName = @"Flat_Home_Icon.png";
babyItem.textDescription = nil;
babyItem.highResImage = @"newText_homehighRes.jpg";
babyItem.newItem = NO;
babyItem.date = nil;
babyItem.nativeItem = YES;
babyItem.photoID = @-1;
[child.items addObject:babyItem];
}
}
}

@end

BabyMilestone 类(从未调用过编码器)

#import "babyMilestone.h"
@implementation babyMilestone

-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self=[super init])) {
self.textDescription = [aDecoder decodeObjectForKey:@"TextDescription"];
self.text = [aDecoder decodeObjectForKey:@"Text"];
self.photoID = [aDecoder decodeObjectForKey:@"PhotoID1"];
self.imageName = [aDecoder decodeObjectForKey:@"Thumbnail"];
self.date = [aDecoder decodeObjectForKey:@"Date"];
self.backgroundImageName = [aDecoder decodeObjectForKey:@"BackgroundImage"];
self.highResImage = [aDecoder decodeObjectForKey:@"HighResImage"];
self.nativeItem = [aDecoder decodeBoolForKey:@"NativeItem"];

}
return self;
}

-(void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.textDescription forKey:@"TextDescription"];
[aCoder encodeObject:self.text forKey:@"Text"];
[aCoder encodeObject:self.photoID forKey:@"PhotoID1"];
[aCoder encodeObject:self.imageName forKey:@"Thumbnail"];
[aCoder encodeObject:self.date forKey:@"Date"];
[aCoder encodeObject:self.backgroundImageName forKey:@"BackgroundImage"];
[aCoder encodeObject:self.highResImage forKey: @"HighResImage"];
[aCoder encodeBool:self.nativeItem forKey:@"NativeItem"];
NSLog(@"In encode with coder in babymilestone.m");
}

@end

子类

#import "Child.h"

@implementation Child
- (id)init { //Used when user add a new child
if ((self = [super init])) {
self.items = [[NSMutableArray alloc] initWithCapacity:20];
}
return self; }

-(id)initWithCoder:(NSCoder *)aDecoder //Load existing children
{
if ((self=[super init])) {
self.name = [aDecoder decodeObjectForKey:@"Name"];
//self.birthday = [aDecoder decodeObjectForKey:@"Birthday"];
NSLog(@"Init with coder being called on Child class");
}
return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"Name"];
NSLog(@"Encodewithcoder is called in the Child class");
//[aCoder encodeObject:self.birthday forKey: @"Birthday];
}
@end

最佳答案

您的 Child 对象作为 babyMilestones 的守护者,负责对它们进行编码。添加 [aCoder encodeObject:self.items forKey:@"Items"];ChildencodeWithCoder:。这样,当告诉 Child 进行编码时,它也会对其 babyMilestones 进行编码。

关于ios - 使用未在较低级别对象上调用的编码器进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655003/

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