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
數組。
+1的鷹眼睛。 – Mundi
您可能想指出,最好使用#define宏或等效宏來確保在兩個地方使用相同的密鑰。 – godel9
你是個天才! –