0

我有几个问题和一些问题:

我想做的事:

将 MyApp.app/Assets/Debug/debug.xml 中的文件重命名为 MyApp.app/Assets/Debug/debug_2.xml

1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml" inDirectory:@"Assets"]; returns (null)

2. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml" inDirectory:@"Debug"]; returns (null)

3. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml"]; WORKS(it finds the file EVEN if its not in the .app its in .app/Assets/Debug.xml)

但:

    if ([fm fileExistsAtPath:path]) {

NSString *newPath = [[NSBundle mainBundle] pathForResource:@"newfile" ofType:@"xml"];   

            [fm copyItemAtPath:path toPath:newPath error:NULL];

        } else {
            NSLog(@"File does not exists");
        }

代码总是抛出这个错误:

-[NSFileManager copyItemAtPath:toPath:error:]: destination path is nil'

当然文件不存在,但它与文件路径相同的路径..只是用另一个名字:)

我想用相同的路径重命名 .app 文件夹中的文件,只是另一个名称。

任何帮助表示赞赏!

同样在上面的示例 1 和 2 中:如果我使用 inDirectory 参数,为什么它会给我空路径?我在构建资源中添加了一个文件夹等..我测试了所有

谢谢

4

3 回答 3

2

您不能修改应用程序包中的资源。它本质上是只读的。这些方法仅返回现有文件pathForResource的路径。

您必须改为将任何新文件写入文档或缓存目录。您将它们放在哪里取决于它们是否可以重新生成以及是否应该备份它们。有关详细信息,请参阅此文档

于 2012-11-19T14:58:50.493 回答
0

您需要阅读开发人员手册以了解您使用的方法...

我为你解读一下:

pathForResource:ofType:inDirectory:

如果 inDirectory 为 nil,则此方法搜索顶级非本地化资源目录和任何特定语言目录的顶级。[cut] 例如,假设您有一个带有现代包的 Mac OS X 应用程序,并且您为 subpath 参数指定@"Assets"。此方法将首先查看捆绑包的 Contents/Resources/Assets目录...

并且您的文件不在Contents/Resources/Assets中。

pathForResource:不适用于不存在的文件。

你需要做什么:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"debug" ofType:"xml"];
NSString *newPath = [filePath stringByDeletingLastPathComponent];
newPath = [newPath stringByAppendingPathComponent:@"debug_2.xml"];
于 2012-11-19T17:51:45.543 回答
0

如果要在应用沙箱中创建目录:

applicationDataFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //OR NSDocumentsDirectory
    NSString* libraryFolderPath = [[[MAFCore sharedInstance] applicationDataFolder] stringByAppendingPathComponent:@"myFolder"];
    BOOL dirExists = [[NSFileManager defaultManager] fileExistsAtPath:libraryFolderPath];
    if(dirExists == NO){
        [[NSFileManager defaultManager] createDirectoryAtPath:libraryFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
    }
于 2012-11-19T15:08:58.287 回答