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 パラメータを使用すると null パスが返されるのはなぜですか? ビルドリソースなどにフォルダーを追加しました..すべてをテストしました

ありがとう

4

3 に答える 3

2

アプリケーション バンドル内のリソースは変更できません。基本的に読み取り専用です。メソッドは、既存のpathForResourceファイルのパスのみを返します。

代わりに、新しいファイルをドキュメントまたはキャッシュ ディレクトリに書き込む必要があります。それらを正確にどこに置くかは、再生成できるかどうか、およびバックアップする必要があるかどうかによって異なります。詳細については、このドキュメントを参照してください。

于 2012-11-19T14:58:50.493 に答える
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 に答える
0

使用するメソッドの開発者マニュアルを読む必要があります...

私はあなたのためにそれを解釈します:

pathForResource:ofType:inDirectory:

inDirectory が nil の場合、このメソッドは最上位のローカライズされていないリソース ディレクトリと、言語固有のディレクトリの最上位を検索します。[カット] たとえば、最新のバンドルを備えた Mac OS X アプリケーションがあり、サブパス パラメーターに@"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 に答える