Objective c NSURL在模拟器中为空,但在iPad上可以

Objective c NSURL在模拟器中为空,但在iPad上可以,objective-c,ipad,ios,nsurl,ios-simulator,Objective C,Ipad,Ios,Nsurl,Ios Simulator,我正在尝试将音频文件加载到iPad上的AVAudioPlayer中。当我在iPad上运行它时,它会发现它在捆绑包中。但是,如果我尝试在模拟器中运行它,那么NSURL将得到一个空错误。下面是代码片段(num是任意int): 在simluator中,调试器控制台跟踪以下内容: 名称=st-answermachine-1 path=/Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39

我正在尝试将音频文件加载到iPad上的AVAudioPlayer中。当我在iPad上运行它时,它会发现它在捆绑包中。但是,如果我尝试在模拟器中运行它,那么NSURL将得到一个空错误。下面是代码片段(num是任意int):

在simluator中,调试器控制台跟踪以下内容:

名称=st-answermachine-1

path=/Users/joe/Library/Application Support/iPhone Simulator/3.2/Applications/B85E9CC8-6E39-47B9-XXXX-1E3A2CE145D1/MyApp.app/st-answermachine-1.m4a

url=(空)

但如果我在设备上尝试,我会得到:

名称=st-answermachine-1

path=/var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

url=/var/mobile/Applications/116DA1CB-EA13-4B80-XXXX-EBD46C8E2095/MyApp.app/st-answermachine-1.m4a

你知道我为什么会有这个问题吗


谢谢

URLWithString:
需要一个包含实际URL的字符串作为其参数(例如'http://blah/“或者”file:///blah'). URL不能包含空格(就像模拟器的路径一样),这就是它失败的原因


正如Evan所建议的,您需要使用
fileURLWithPath:
将路径字符串转换为URL对象。

您尝试过
fileURLWithPath:
而不是
URLWithString:
?不难,Joe。这是一个很好的、直截了当的问题……我认为它和答案也指向了我代码中的一个bug。
NSString *name = [NSString stringWithFormat:@"st-answermachine-%i", num];
NSLog(@"name = %@", name);
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"m4a"];
NSLog(@"path = %@", path);
NSURL *url = [NSURL URLWithString:path];
NSLog(@"url = %@", url);