所以你使用 writeToFile:atomically: 方法将 NSDictionary 的数据写入文件,对吗?像下面这样:
static NSString *filename = @"DictFile.plist";
- (NSDictionary *)openDict
{
NSString *filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:filename];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
return dict;
}
- (void)saveDict:(NSDictionary *)dict
{
NSString *filePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:filename];
if (![dict writeToFile:filePath atomically:YES])
{
[[NSException exceptionWithName:@"IOException"
reason:[NSString stringWithFormat:@"filename.plist cannot be saved to %@", filePath]
userInfo:nil] raise];
}
NSLog(@"File on path: %@", filePath);
}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
检查方法:
- (NSDictionary *)createDict
{
NSDictionary *dict = [self openDict];
if (dict)
{
id key = [[dict allKeys] firstObject];
if (key)
{
NSLog(@"Found dictionary: %@ - %@", key, [dict objectForKey:key]);
}
}
else
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Key", @"Value", nil];
[self saveDict:dict];
}
return dict;
}