1

選択したサウンド ファイルをアプリのディレクトリにコピーするアプリを作成しようとしています。それを行うために、私は次のコードを書きました:

NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setAllowsMultipleSelection:NO];
[openDlg setCanChooseDirectories:NO];
[openDlg setAllowedFileTypes:[NSArray arrayWithObjects:@"aif",@"aiff",@"mp3",@"wav",@"m4a",nil]];

if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dataPath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"Datapath is %@", dataPath);
    NSLog(@"Selected Files : %@",[[openDlg URLs] objectAtIndex:0]);
    if ([fileManager fileExistsAtPath:dataPath] == NO)
    {
        [fileManager copyItemAtPath:[[openDlg URLs] objectAtIndex:0] toPath:[[NSBundle mainBundle] bundlePath] error:&error];
        NSLog(@"File copied");

    }
}

問題は、各タイプのファイル (aif、wav、mp3 などだけでなく) を選択できることですFile copied。ただし、パスは正しいです。if ステートメントを削除すると、 : というエラーが表示されます[NSURL fileSystemRepresentation]: unrecognized selector sent to instance 0x1005a0b90。このコードのどこが間違っていますか?

4

1 に答える 1

1

NSURL内のパスを期待する API に を渡していますNSString。URL ベースの API を使用することを検討してください。

- (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error NS_AVAILABLE(10_6, 4_0);

このような:

[fileManager copyItemAtURL: [[openDlg URLs] objectAtIndex:0] toURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]] error:&error];

[fileManager fileExistsAtPath:dataPath]また、説明が戻ってくることを示しているため、ファイルは既にバンドルにコピーされているとNO思います(NSLogが実行されないため)。手動で確認するか、NSFileManager前に既存のファイルを削除するように依頼できます。新しく選択したファイルにコピーしています。

于 2013-05-03T10:52:00.523 に答える