objective-c 使用来自 [[NSBundle mainBundle] resourcePath] 的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:39:31  来源:igfitidea点击:

Working with paths from [[NSBundle mainBundle] resourcePath]

objective-cdirectorypathnsbundle

提问by Tom Irving

I'm sure it's a very basic problem, but I'm having trouble finding anything about it.

我敢肯定这是一个非常基本的问题,但我很难找到有关它的任何信息。

Say I've got my app in a folder, in some more folders, like this:

假设我将我的应用程序放在一个文件夹中,在更多文件夹中,如下所示:

  • MainFolder > SecondaryFolder > AppBundle.app > all the stuff
  • MainFolder > SecondaryFolder > AppBundle.app > 所有的东西

Then, what I want to do is access a file that is in the "MainFolder". I know I can get the path of the AppBundle by using:

然后,我想要做的是访问“MainFolder”中的文件。我知道我可以使用以下方法获取 AppBundle 的路径:

NSLog(@"%@",[[NSBundle mainBundle] resourcePath]);

NSLog(@"%@",[[NSBundle mainBundle] resourcePath]);

What I'm uncertain about is how to get the path of the "MainFolder".

我不确定的是如何获取“MainFolder”的路径。

Any pointers would be great!

任何指针都会很棒!

Thanks, Tom

谢谢,汤姆

回答by pix0r

I'd use -[NSString stringByDeletingLastPathComponent]twice.

我会用-[NSString stringByDeletingLastPathComponent]两次。

NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSString *secondParentPath = [[bundlePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];

回答by Constantine

I use: NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];

我用: NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];