我正在尝试创建一个 Mac OS X 应用程序,其中有一些默认声音,用户可以根据需要添加其他声音。我正在将声音加载到数组中-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 string parameter
。我猜它找不到文件的 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];
}
这段代码有什么问题?