1

このアプリをデバッグしようとしていますが、大きな問題が 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

4

1 に答える 1

3

アーカイブとアンアーカイブの際に同じキーを使用するのに役立ちます:

self.notifcationStatus = [aDecoder decodeBoolForKey:@"notifcationStatus"];

...

[aCoder encodeBool:self.notifcationStatus forKey:@"notificationStatus"];

notifcationStatusデコード時とエンコード時の2つの異なるキーを使用していますnotificationStatus。( iがありません)。

この状況では、 #define マクロまたは同等のマクロを使用して、両方の場所で同じキーが使用されるようにすることをお勧めします (帽子のヒント: @godel9):

// somewhere in your .h, for instance:
#define kNotificationStatus @"notificationStatus"


self.notifcationStatus = [aDecoder decodeBoolForKey: kNotificationStatus];

...

[aCoder encodeBool:self.notifcationStatus forKey: kNotificationStatus];
于 2013-10-27T15:28:16.510 に答える