写给plist但是当我放弃模拟时它是空的

您好我执行此对象PlistManager写/读plist在我的项目。问题是plist填充罚款将运行(我希望),当进入后台(我使用通知和NSlog [Plistmanager readPlist:@“Database” ])我仍然可以读取之前输入的数据。

重点是当我停止对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

任何人都可以解释什么是错的? 谢谢

0
额外 编辑
意见: 1
正面。如果你问我?
额外 作者 luca,
你知道 NSDictionary writeToFile方法返回成功结果?检查是否为YES或NO。另外,你在你的字典中记录什么类型的东西?这些项目中的任何一个是否定制了Objective C对象?
额外 作者 Michael Dautermann,
你在使用ARC吗?
额外 作者 CarlJ,

1 答案

你什么时候打电话把数据保存到plist?如果您从Xcode退出应用程序,那么它会立即终止,并且不会通过任何与终止或输入背景有关的应用程序委托方法,这是我怀疑您的代码的地方。

按下模拟器上的主页按钮即可让这些方法运行。

0
额外