【问题标题】:NSURL is null in Simulator, but okay on iPadNSURL 在模拟器中为空,但在 iPad 上没问题
【发布时间】:2011-05-10 19:50:51
【问题描述】:

我正在尝试将音频文件加载到 iPad 上的 AVAudioPlayer。当我在 iPad 上运行它时,它可以在捆绑包中找到它。但是,如果我尝试通过模拟器运行它,我会收到 NSURL 的空错误。这是代码的 sn-p(num 是任意 int):

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);

在模拟器中,调试器控制台会跟踪:

名称 = 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 = (null)

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

名称 = st-answermachine-1

路径 = /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

请告诉我为什么我可能会遇到这个问题?

谢谢!

【问题讨论】:

  • 你试过fileURLWithPath:而不是URLWithString:吗?

标签: objective-c ipad ios nsurl ios-simulator


【解决方案1】:

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

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

【讨论】:

  • 不难,乔。这是一个很好的、直截了当的问题……我认为它和答案也指向了我的代码中的一个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2012-06-28
  • 2012-10-25
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多