Ios 将带有自定义对象的NSDictionary写入文件
我有一个带有自定义对象数组的Ios 将带有自定义对象的NSDictionary写入文件,ios,objective-c,nsdictionary,nsobject,Ios,Objective C,Nsdictionary,Nsobject,我有一个带有自定义对象数组的NSDictionary,名为MAClasses。我尝试将dict写入文件,这是一种简单的方法: - (void)writeDictToFileWithContent:(NSDictionary *)contentDict { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docume
NSDictionary
,名为MAClass
es。我尝试将dict写入文件,这是一种简单的方法:
- (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;
它代表一个类
我以前只使用NSString和NSNumber的数组,它可以很好地读写。当我切换MAClass对象时,当我尝试读取时,它返回一个空的dict
这可能就是为什么:
我该怎么写dict呢?你需要序列化你的自定义对象,一种方法是,在你的自定义类中,编写以下两个方法,对你想要保存的每个属性进行编码/解码,然后我不确定writeToFile是否真的会使用它,但我知道您可以从对象创建NSData,并使用以下方法将其写入文件
//archive dictionary and serializable objects to NSData
NSData *serializedData= [NSKeyedArchiver archivedDataWithRootObject:myDict];
//Unarchive
NSData *myData=[NSData dataWithContentsOfFile:appFile];
NSMutableDictionary* myDict= (NSMutableDictionary*)[NSKeyedUnarchiver unarchiveObjectWithData:myData];
以下是自定义对象中需要的两种方法:
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInt:self.propOne forKey:@"PropOne"];
[coder encodeObject:self.propTwo forKey:@"PropTwo"];
...
}
// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
self.propOne=[coder decodeIntegerForKey:@"PropOne"];
self.propTwo=[coder decodeObjectForKey:@"PropTwo"];
..
return self;
}
希望对您有所帮助您无法使用自定义对象保存nsdictionary。尝试将nsdictionary以编码形式保存到文件中并检索词典,然后对其进行解码。那我想你会得到你的目标的。 试试这个
- (void)writeDictToFileWithContent:(NSDictionary *)contentDict {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/userdata.txt"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:contentDict];
[data 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 [NSKeyedUnarchiver unarchiveObjectWithData:contentDict];;
}
-[MAClass encodeWithCoder:]:发送到实例0xba92af0的无法识别的选择器
我是否需要在myMAClass
NSObject中添加一个encode WithCoder方法?是的,您还必须实现这些方法,或者您必须从文件中读取存档数据是NSData*storedData=[NSData data WITHCONTENTSOFILE:[self myDataFilePath]]; 返回[NSKeyedUnarchiver unarchiveObjectWithData:storedData];您建议如何制作对象和INT?我不完全理解整个PropOne
,PropTwo
。@user3451350 PropOne/PropTwo是你的类的属性,我只是把它们命名为示例…-(void)encodeWithCoder:(NSCoder*)encoder{[encoder-encoder-object:self-forKey:@“self”;}
-(id)initWithCoder:(NSCoder*))解码器{if(self=[super init]){self=[decoder decodeObjectForKey:@“self”];}返回self;}
这些方法有效吗???@user3451350不,你不能解码self…你的对象中有属性,你在问题中列出了它们,现在只需写一行[coder-encodeObject:self.propTwo-forKey:@“PropTwo”];对于每个属性(您的所有属性都是对象),顺便说一句,您的属性是日期、分配名称、总点数等