gpt4 book ai didi

ios - 将带有自定义对象的 NSDictionary 写入文件

转载 作者:行者123 更新时间:2023-11-29 12:44:43 25 4
gpt4 key购买 nike

<分区>

我有一个 NSDictionary,其中包含一组自定义对象,称为 MAClasses。我尝试将字典写入文件,简单方法:

- (void)writeDictToFileWithContent:(NSDictionary *)contentDict {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/userdata.txt"];


[contentDict writeToFile:filePath atomically:YES];

}

然后我阅读使用

- (NSDictionary *)readFromDict {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/userdata.txt"];

NSDictionary *contentDict = [NSDictionary dictionaryWithContentsOfFile:filePath];

return contentDict;
}

我尝试编写的对象数组是一个自定义的 NSObject,它的结构有点像这样:

@interface MAAssignment : NSObject

@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSString *assignmentName;
@property (nonatomic, strong) NSNumber *totalPoints;
@property (nonatomic, strong) NSNumber *recievedPoints;
@property (nonatomic, strong) NSNumber *classAverage;
@property (nonatomic, strong) NSNumber *extraCredit;
@property (nonatomic, strong) NSNumber *notGraded;

代表一个类。

我之前只使用 nsstrings 和 nsnumbers 数组,它可以很好地读写。当我切换 MAClass 对象时,它在我尝试读取时返回了一个空字典。

这可能是为什么:iPhone - Writing NSMutableDictionary to file

我应该怎么做才能改为编写字典?

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