Question

here is the code i am working with.

NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];

if(path == nil)
{
NSLog(@"file not found");
}

when i run this it prints file not found in the console.

my file.plist is in the supporting files folder in my cocoa project. where does mainBundle look for files at exactly. this is stumping me quite a bit. i get that it looks for the .app file, but when developing the app where doe mainBundle look?

Was it helpful?

Solution

pathForResource:ofType: only looks in the Resources folder (and localised folders). If you are placing files into dedicated folders in the bundle then you need to use pathForResource:ofType:inDirectory: and supply the directory name.

OTHER TIPS

If the NSBundle you found is what you wanted through :

NSBundle *yourTargetBundle = [NSBundle mainBundle];

or

NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] 
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];

And get the nil result use function: pathForResource: ofType:. Then you can log the paths of resource under yourTargetBundle:

// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);

it will log all the resources path of type png. You can get the image object use:

targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];

I think the above approach is one way to resolve the return nil situation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top