代码之家  ›  专栏  ›  技术社区  ›  Abdullah Shafique

存档阵列后BOOL变为false

  •  1
  • Abdullah Shafique  · 技术社区  · 11 年前

    我正在尝试调试这个应用程序,但有一个大问题。当我试图将数组保存到数据文件时,一切都正常。然而,如果我关闭应用程序并重新打开,数组中的布尔值将变为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 .

    1 回复  |  直到 11 年前
        1
  •  3
  •   magma    11 年前

    它有助于在归档和取消归档时使用相同的密钥:

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

    您正在使用两个不同的密钥: notifcationStatus 当解码时,以及 notificationStatus 在编码时。(少了一个 ).

    在这种情况下,最好使用#define宏或等效宏,以确保在两个位置使用相同的键(帽子提示:@godel9):

    // somewhere in your .h, for instance:
    #define kNotificationStatus @"notificationStatus"
    
    
    self.notifcationStatus = [aDecoder decodeBoolForKey: kNotificationStatus];
    
    ...
    
    [aCoder encodeBool:self.notifcationStatus forKey: kNotificationStatus];