Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 将用户提供的数据添加到数组-Objective-C_File_Nsurl - Fatal编程技术网

File 将用户提供的数据添加到数组-Objective-C

File 将用户提供的数据添加到数组-Objective-C,file,nsurl,File,Nsurl,我正在尝试创建一个MacOSX应用程序,其中有一些默认声音,用户可以根据需要添加其他声音。我正在将声音加载到-awakeFromNib中的数组中: for (NSString *str in [PreferencesController beats]) { resourcePath = [[NSBundle mainBundle] pathForSoundResource:str]; beat = [[NSSound alloc] initWithContentsOfFile:r

我正在尝试创建一个MacOSX应用程序,其中有一些默认声音,用户可以根据需要添加其他声音。我正在将声音加载到
-awakeFromNib
中的数组中:

for (NSString *str in [PreferencesController beats]) {
    resourcePath = [[NSBundle mainBundle] pathForSoundResource:str];
    beat = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES];
    [beat setLoops:YES];
    [beat setName:str];
    [beatsArray addObject:beat];
}
在应用程序尝试向阵列中添加用户添加的声音之前,一切正常。它说:
***-[NSURL initFileURLWithPath:]:nil字符串参数
。我猜它找不到文件的URL,但当用户通过以下代码选择该文件时,我正在将该文件复制到应用程序的目录中:

if ( [openDlg runModalForTypes:[NSArray arrayWithObjects:@"aif",@"aiff",@"mp3",@"wav",@"m4a",nil]] == NSOKButton)
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;

    NSString *dataPath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"Datapath is %@", dataPath);
    NSLog(@"Selected Files : %@",[[openDlg URLs] objectAtIndex:0]);

    [fileManager copyItemAtURL: [[openDlg URLs] objectAtIndex:0] toURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]] error:&error];
        NSLog(@"File copied");
    NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:[PreferencesController beats]];
    NSString *fileName = [[[openDlg URL] path] lastPathComponent];
    NSArray *fileNameArray = [fileName componentsSeparatedByString:@"."];
    [newArray addObject:[fileNameArray objectAtIndex:0]];
    NSLog(@"%@",newArray);
    [PreferencesController setBeats:newArray];
    [self awakeFromNib];
    [_tableView reloadData];
}

此代码有什么问题?

看起来您正试图将文件复制到文件夹,因为您只是将捆绑路径用作目标URL。目标URL需要指定包含目标文件名的完整路径

复制文件时尝试记录错误:

NSLog(@"File copied with error: %@", error);
此行包含错误:

[fileManager copyItemAtURL: [[openDlg URLs] objectAtIndex:0] toURL: [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]] error:&error];
具体而言,问题在于目标URL是一个文件夹:

[[NSBundle mainBundle] bundlePath]
应该是这样的:

[[[NSBundle mainBundle] bundlePath] stringByAppendingString:[[[[openDlg URLs] objectAtIndex:0] path] lastPathComponent];
然后,正如@Abizern在评论中所说的,一旦你让它工作起来,保存到bundle是个坏主意(它是应用程序的一部分)。
一旦开始工作,您应该选择一个更好的位置将声音保存到(如应用程序的支持文档文件夹)。

上面写着:
Error Domain=NSCocoaErrorDomain code=516“1音轨”无法复制到“Debug”,因为已经存在同名的项。“
但是如果文件存在,它应该能够在数组no?中加载声音,如果文件夹中已存在该项,则不能加载。还可以尝试记录您试图加载以播放声音的路径,并查看是否确实存在声音文件。好的,忽略记录,只需修复保存文件的路径即可。写入应用程序捆绑包是一个坏主意。@user2331955用户不必具有写入应用程序捆绑包的权限。如果他们这样做了,他们向其中写入可能会破坏您的应用程序、更改其行为或破坏其代码签名标识。您正在尝试写入您的应用程序包吗?改为尝试写入文件系统。