On iOS, is there a quicker way to get the URL of the mainBundle resource?
cocoa-touch, foundation, ios, objective-c
Solution
How about:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"0" withExtension:@"aiff"];
Starting from there, you could also write a category for `NSURL` or `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
Problem
It seems a bit strange that we have to use such a long way to get to a file's path: ``` NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"0" ofType:@"aiff"]]; ``` I wonder is there a faster way, such as a class method for `NSURL` or `NSBundle`? ``` NSURL *soundURL = [NSURL urlFromMainBundle:@"0.aiff"]; ``` or ``` NSURL *soundURL = [NSBundle urlFromMainBundle:@"0.aiff"]; ``` If not, is there a reason why it is not a good form?