アプリの起動間でプロパティの状態を保持するために、 NSKeyedArchiver
/を使用してクラスのプロパティのデータをファイルに保存しようとしています。NSKeyedUnarchiver
I have the property declared in MyClass.h like so:
@property (nonatomic, weak) NSDictionary *myDictionary;
and I've created custom getter and setter methods in MyClass.m to make sure the NSDictionary is written to disk:
-(NSDictionary *)myDictionary {
NSDictionary *dictionary;
NSString *path = [self pathToDataStore];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
dictionary = [NSDictionary dictionary];
} else {
NSData *archivedData = [NSData dataWithContentsOfFile:path];
dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
return dictionary;
}
-(void)setMyDictionary:(NSDictionary *)dictionary {
NSString *path = [self pathToDataStore];
NSDictionary *rootObject = [NSDictionary dictionaryWithDictionary:dictionary];
[NSKeyedArchiver archiveRootObject:rootObject toFile:path];
self.myDictionary = dictionary;
}
This is causing an infinite loop of calls to [self setMyDictionary]
, so clearly I'm doing something wrong. But what is the problem?