gpt4 book ai didi

ios - 归档数组后 BOOL 变为 false

转载 作者:行者123 更新时间:2023-12-01 19:05:57 27 4
gpt4 key购买 nike

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

最佳答案

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

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

...

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

您正在使用两个不同的 key : notifcationStatus解码时, notificationStatus编码时。 (缺少 i )。

在这种情况下,最好使用 #define 宏或等效宏来确保在两个地方使用相同的键(帽子提示:@godel9):
// somewhere in your .h, for instance:
#define kNotificationStatus @"notificationStatus"


self.notifcationStatus = [aDecoder decodeBoolForKey: kNotificationStatus];

...

[aCoder encodeBool:self.notifcationStatus forKey: kNotificationStatus];

关于ios - 归档数组后 BOOL 变为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com