このアプリをデバッグしようとしていますが、大きな問題が 1 つあります。配列をデータ ファイルに保存しようとすると、すべてが機能します。ただし、アプリを閉じて再度開くと、配列内のブール値が nil になります。配列を保存するコードは次のとおりです。
NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
NSLog(@"%@", self.alist.description);
- (NSString*)dataFilePath
{
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"AssignmentInfo.data"];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (!file) {
if (![[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]) {
}
else
file = [NSFileHandle fileHandleForWritingAtPath:filePath];
}
return filePath;
}
配列内には、私が作成したカスタム クラスがあります。クラスのコードは次のとおりです。
-(NSString *)description
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: self.dateTime];
return [NSString stringWithFormat:@"Class: %@\r Assignment Title: %@ \rAssignment Description: %@ \rDue: %@ \r%s", self.className, self.assignmentTitle, self.assignmentDescription, dateTimeString,self.notifcationStatus ? "Notification On" : "Notification Off"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
self.className = [aDecoder decodeObjectForKey:@"className"];
self.assignmentTitle = [aDecoder decodeObjectForKey:@"assignmentTitle"];
self.assignmentDescription = [aDecoder decodeObjectForKey:@"assignmentDescription"];
self.dateTime = [aDecoder decodeObjectForKey:@"dateTime"];
self.notifcationStatus = [aDecoder decodeBoolForKey:@"notifcationStatus"];
return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.className forKey:@"className"];
[aCoder encodeObject:self.assignmentTitle forKey:@"assignmentTitle"];
[aCoder encodeObject:self.assignmentDescription forKey:@"assignmentDescription"];
[aCoder encodeObject:self.dateTime forKey:@"dateTime"];
[aCoder encodeBool:self.notifcationStatus forKey:@"notificationStatus"];
}
self.notifcationStatus
になる配列ですFALSE
。