我们必须使用如此长的路才能到达文件的路径,这似乎有点奇怪:
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"0" ofType:@"aiff"]];
我想知道有更快的方法,例如NSURL
或NSBundle
的类方法吗?
NSURL *soundURL = [NSURL urlFromMainBundle:@"0.aiff"];
或
NSURL *soundURL = [NSBundle urlFromMainBundle:@"0.aiff"];
如果没有,是否有理由说它不是一个好形式?
答案 0 :(得分:5)
怎么样:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"0" withExtension:@"aiff"];
从那里开始,您还可以为NSURL
或NSBundle
写一个类别:
@interface NSURL (NSURL+Resource)
+ (NSURL *)URLForResource:(NSString *)resource;
@end
@implementation NSURL (NSURL+Resource)
+ (NSURL *)URLForResource:(NSString *)resource
{
NSString *name = [resource stringByDeletingPathExtension];
NSString *extension = [resource pathExtension];
return [[NSBundle mainBundle] URLForResource:name withExtension:extension];
}
@end
答案 1 :(得分:1)
您可以在NSBundle
课程中使用URLForResource:withExtension:
:
NSURL *soundURL = [[NSBundle mainBundle]URLForResource:@"0" withExtension:@"aiff"];
答案 2 :(得分:1)
您可以尝试:
NSURL *soundURL = [[NSBundle mainBundle]URLForResource:@"0" withExtension:@"aiff"];