ios 在应用程序启动时获取包文件引用/路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12205785/
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-08-30 19:57:13  来源:igfitidea点击:

Getting bundle file references / paths at app launch

iosios5nsfilemanagernsbundle

提问by Andrew Lauer Barinov

Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard.

假设我在 Main App Bundle 中包含了一组任意文件。我想在启动时为那些获取文件 URL 并将它们存储在某个地方。这可以使用NSFileManager吗?文件在这方面不清楚。

Note: I only need the file URLs, I do not need to access the actual files.

注意:我只需要文件 URL,不需要访问实际文件。

回答by

You can get the URL of a file in the main bundle using

您可以使用以下命令获取主包中文件的 URL

NSString *path = [[NSBundle mainBundle] pathForResource:@"SomeFile" ofType:@"jpeg"];
NSURL *url = [NSURL fileURLWithPath:path];

You can write this URL to, for example, a property list file in the Documents directory:

例如,您可以将此 URL 写入 Documents 目录中的属性列表文件:

NSString *docsDir = [NSSearchForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [docsDir stringByAppendingPathComponent:@"Files.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[url absoluteString] forKey:@"SomeFile.jpeg"];
[dict writeToFile:plistPath atomically:YES];

If you don't know the names of the files and you just want to list all the files in the bundle, use

如果您不知道文件的名称,而只想列出捆绑包中的所有文件,请使用

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] bundlePath] error:NULL];
for (NSString *fileName in files) {
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:path];
    // do something with `url`
}

回答by Bersaelor

Or in Swift 4:

或者在 Swift 4 中:

        let url = Bundle.main.url(forResource: "FileName", withExtension: ".xyz")

回答by David H

Yes, you would get their path:

是的,你会得到他们的路径:

NSString *path = [NSBundle mainBundle] pathForResource:@"file1" ofType:@"png"];
NSURL *fileURL = [NSURL fileURLWithPath:path]

// save it as any other object or in a dictionary:

[myMutableDictionary setObject:fileURL forKey:@"file1.png"];

EDIT: to get the complete list of files, use the NSFileManager, get the path to the bundle itself, then walk each directory getting the files, making URLs, and saving them somewhere. There is oodles of code on SO how to walk a directory to do this. [You should update your question to be more specific on what you want, this was not made clear at all originally]

编辑:要获取文件的完整列表,请使用 NSFileManager,获取包本身的路径,然后遍历每个目录以获取文件、制作 URL 并将它们保存在某处。关于如何遍历目录来执行此操作有大量代码。[您应该更新您的问题以更具体地说明您想要什么,这最初根本没有说清楚]

回答by Dov

There's an API on NSBundle(documentation) available on 10.6 or iOS 4 to get an NSURLdirectly, rather than passing a path to the NSURLconstructor:

在 10.6 或 iOS 4上NSBundle文档)有一个 API可以NSURL直接获取,而不是将路径传递给NSURL构造函数:

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)ext;

It also has variants that take a subdirectoryand localizationName, the same as its -pathForResource:equivalents.

它还具有带 asubdirectory和 的变体,localizationName与其-pathForResource:等价物相同。