0

嗨,我实现了这个对象 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

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

4

1 回答 1

0

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

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

于 2012-05-25T06:41:00.187 回答