写入plist但是当我退出模拟时它是空的

时间:2012-05-25 06:20:41

标签: iphone xcode plist

嗨我实现了这个对象PlistManager在我的项目中写/读plist。问题是plist填充正常运行(我希望)并且在后台输入时(我使用了通知和NSlog [Plistmanager readPlist:@] “数据库”])我仍然可以读取之前输入的数据。

关键是当我停止xcode上的模拟并再次运行时,plist现在为空。为什么?

@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;
}

解决

我刚刚发现了这些用于读/写plist的新方法,它正在工作。我不得不在appdelegate(didFinishLaunchingWithOptions)中添加“initializeFileDic”方法:

@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(@"%@ \n,\n %@ ",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\n\n");
    else
        NSLog(@"Error while trying to copy into DocumentsDirectory\n\n"); 
    }

}


@end

有人可以解释什么是错的吗? 感谢

1 个答案:

答案 0 :(得分:0)

您何时拨打电话将数据保存到plist?如果您从Xcode退出应用程序,那么它会立即终止,并且不会通过任何与终止或进入后台相关的应用程序委托方法,这是我怀疑您的代码所在。

按下模拟器上的主页按钮是获取这些方法的方法。