0

我在 Application Support 文件夹中创建了一个包含默认值的WinDef.plist文件。我想将此文件添加到我的项目中,而无需手动执行。

在此处输入图像描述

知道我该怎么做吗?

罗纳德

4

1 回答 1

2

在您的应用程序中尝试以下方法 didfinishlaunching 方法

-(void) checkAndCreateFile
    {
        //-------------------------------------------
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:cFilePath];
        //-------------------------------------------
        //File already there
        if(success)
        {
            return;
        }
        //-------------------------------------------
        //create file
        NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:cfileName];
        [fileManager copyItemAtPath:filePathFromApp toPath:cfilePath error:nil];

    }

//------------------------------------------------------------------------
// Method : copyFile
// Method to create file
//------------------------------------------------------------------------
-(void) copyFile
{

        cfileName = @"WinDef.plist";

    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentsPaths objectAtIndex:0];
    cfilePath = [documentDir stringByAppendingPathComponent:cfileName];
    [self checkAndCreateFile];

}

如果需要,这会将 WinDef.plist 文件保存在您的应用程序文件夹中

如果您想访问该文件值,则可以使用以下代码检索它

NSString *path = [[NSBundle mainBundle] pathForResource: @"WinDef" ofType: @"plist"]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile: path];
id obj1 = [dictionary objectForKey: @"YourKey"];

这将为您提供字典中的键值

于 2012-11-26T06:12:11.767 回答