我試圖創建一個Mac OS X應用程序,其中有一些默認聲音,用戶可以添加其他人,如果他想要的話。我加載的聲音到一個數組中-awakeFromNib
:直到應用程序試圖添加到陣列的用戶增加了聲音將用戶給出的數據添加到數組 - Objective-C
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];
}
有什麼不對的代碼?
您正試圖寫入您的應用程序包?嘗試寫入文件系統。 – Abizern 2013-05-03 12:39:40