English 中文(简体)
写入列表到列表中, 但当我退出模拟时是空的
原标题:writing to plist but it s empty when I quit simulation

Hi 我在我的项目中应用了这个对象“ 列表管理器” 来写/ 读 列表 。 问题在于该列表会运行( 我希望), 当输入背景时( 我使用通知, NSlog ) 我仍然可以读取之前输入的数据 。

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

@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

问题回答

您何时拨打电话将数据保存到列表中? 如果您退出 Xcode 中的应用程序, 将会被立即终止, 并且不会通过任何与终止或进入背景有关的应用程序代表方法, 我怀疑您的代码所在 。

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





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签