简体   繁体   English

的NSDate <not an Objective-C object>

[英]NSDate <not an Objective-C object>

After writing to archive and reading the NSDate object of custom class comes back as not an Objective-C object: Do I need to switch it to a string or should I be able to store it directly as an object within the class. 写入存档并读取自定义类的NSDate对象后,不再返回Objective-C对象:我是否需要将其切换为字符串,或者我是否可以将其直接存储为类中的对象。

anArray = [[NSMutableArray alloc] initWithObjects:nil];
self.title = @"Homework APP";
NSString *filePath = [self dataFilePath];
NSDate *datetoday=[NSDate date];
//Create an object for archiving
homework *myHomework = [[homework alloc] init];
myHomework.classname = @"Physics";
myHomework.title = @"HW1";
myHomework.description = @"Test";
myHomework.duedate = datetoday;
myHomework.notify = TRUE;
homework *myHomework2 = [[homework alloc] init];
myHomework2.classname = @"Computer";
myHomework2.title = @"HW2";
myHomework2.description = @"Test";
myHomework2.duedate = datetoday;
myHomework2.notify = FALSE;

NSMutableArray *myHomeworksArray = [[NSMutableArray alloc] initWithObjects:myHomework, myHomework2, nil];
anArray = myHomeworksArray;
//Archive my object
[NSKeyedArchiver archiveRootObject:myHomeworksArray toFile:filePath];

//Unarchive my object to check
homework *archivedHomework = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

this is the encoder/decoder: 这是编码器/解码器:

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.classname forKey:@"classname"];
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeObject:self.description forKey:@"description"];
    [aCoder encodeObject:self.duedate forKey:@"duedate"];
    [aCoder encodeBool:self.notify forKey:@"notify"];

}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init]; //call class
    if (self) { //makes sure it exists
    self.classname = [aDecoder decodeObjectForKey:@"classname"];
    self.title = [aDecoder decodeObjectForKey:@"title"];
    self.description = [aDecoder decodeObjectForKey:@"description"];
    self.duedate = [aDecoder decodeObjectForKey:@"duedate"];
    self.notify = [aDecoder decodeBoolForKey:@"notify"];
    }

    return self;

}

您正在归档对象数组,但您尝试仅取消归档一个“作业”对象:

homework *archivedHomework = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM