Hi 我在我的项目中应用了这个对象“ 列表管理器” 来写/ 读 列表 。 问题在于该列表会运行( 我希望), 当输入背景时( 我使用通知, NSlog ) 我仍然可以读取之前输入的数据 。
@interface ReadWritePlist : NSObject
+(void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic;
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist;
@end
@implementation ReadWritePlist
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",filePlist]] atomically:YES];
}
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:@"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
< 坚固 > 解决 < / 坚固 >
我刚刚找到了这些新的读/写列表方法, 它正在起作用。 我不得不在应用门( Finnish Launching with Options) 中添加“ 初始化FileDic ” 方法 :
@implementation PlistManager
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:@"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",filePlist]] atomically:YES]) {
NSLog(@"Ok done!");
}
else{
NSLog(@"Noo not done!");
}
}
+ (void)initializeFileDic:(NSString*)FileNameExt
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *startPath = [[path stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:@"plist"] ;
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectoryPath stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:@"plist"];
NSLog(@"%@
,
%@ ",startPath,filePath);
if([NSDictionary dictionaryWithContentsOfFile: startPath]){
NSLog(@"File %@ exists",filePath);
}
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if([[NSDictionary dictionaryWithContentsOfFile:startPath] writeToFile:filePath atomically:NO])
NSLog(@"File copied inside App DocumentsDirectory
");
else
NSLog(@"Error while trying to copy into DocumentsDirectory
");
}
}
@end
Could Anybody explain what was wrong? Thanks