2013-10-27 30 views
1

我正在嘗試調試此應用程序,但有一個大問題。當我嘗試將我的數組保存到數據文件時,一切正常。但是,如果我關閉應用程序,並重新打開數組中的布爾變成零。下面是代碼保存數組:BOOL在歸檔陣列後變爲假

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數組。

回答

3

它可以幫助使用相同的密鑰,封存和解除封存時:

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]; 
+1

+1的鷹眼睛。 – Mundi

+2

您可能想指出,最好使用#define宏或等效宏來確保在兩個地方使用相同的密鑰。 – godel9

+0

你是個天才! –