문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top