纯资源 NSBundle:这是犹太洁食吗?

发布于 2024-12-05 12:26:13 字数 871 浏览 0 评论 0原文

在我的 iOS 应用程序中,我将网络上的内容下载到我的 /Library/Caches 目录中。我想将此目录表示为 NSBundle,以便与我们正在使用的一些外部 API 更好地兼容。 (这样,只要它出现,我们就可以简单地将 [[NSBundle mainBundle] pathForResource...] 更改为 [myBundle pathForResource...]。)

以下内容似乎工作正常:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachesDirectory = [paths objectAtIndex:0];
NSBundle* bundle = [NSBundle bundleWithPath:cachesDirectory];

更好的是,该捆绑包反映了我对/Library/Caches 目录。但是,我很担心,因为从技术上讲,缓存目录不是每个 Apple 文档。也就是说:

  • 它不是一个“具有标准化层次结构的目录,其中包含可执行代码和该代码使用的资源”,因为没有代码。
  • 它既不是应用程序、框架,也不是插件包。
  • 它最像一个应用程序包,但它不包含所需的 Info.plist 或可执行文件。

我找不到任何地方提到这种动态创建的纯资源包。这可以吗?

In my iOS app, I'm downloading content from the web into my /Library/Caches directory. I'd like to represent this directory as an NSBundle for better compatibility with some of the external APIs we're using. (That way, we can simply change [[NSBundle mainBundle] pathForResource...] to [myBundle pathForResource...] whenever it appears.)

The following seems to work fine:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachesDirectory = [paths objectAtIndex:0];
NSBundle* bundle = [NSBundle bundleWithPath:cachesDirectory];

Better yet, the bundle reflects any changes I make to the /Library/Caches directory. However, I'm concerned because the caches directory is technically not a bundle per Apple's docs. That is to say:

  • It's not a "directory with a standardized hierarchical structure that holds executable code and the resources used by that code", since there's no code.
  • It's neither an Application, Framework, nor a Plug-In bundle.
  • It's most like an Application bundle, but it doesn't contain the required Info.plist or executable.

I could find no mention anywhere of this sort of dynamically-created, resource-only bundle. Is this okay to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他夏了夏天 2024-12-12 12:26:13

/Library/Caches 目录将缺少捆绑包中所需的一些标准文件,例如 Contents/ 目录或 Contents/Info.plist< /code> 文件,因此当将其视为一个文件时,它可能无法正常运行。谨慎行事。

The /Library/Caches directory will lack some of the standard files which are required in a bundle, like a Contents/ directory or a Contents/Info.plist file, so it may not behave properly when treated as one. Proceed with caution.

不如归去 2024-12-12 12:26:13

是的,拥有纯资源包绝对没问题。您引用的一些措辞早在 iOS 中就已存在。在 OS X 中,您可以动态加载可执行代码,这在 iOS 中是明确排除的。

本地化是纯资源捆绑的一个示例。


编辑:

捆绑包编程指南说:

虽然文档格式可以利用捆绑结构来
组织其内容,一般不考虑文档
最纯粹意义上的捆绑。一个被实现为
目录并被视为不透明类型被视为文档
包,无论其内部格式如何。欲了解更多信息
文档包,请参阅“文档包。”

其中说:

有多种方法可以访问文档包的内容。
由于文档包是一个目录,因此您可以访问
使用任何适当的文件系统例程来更改文档的内容。如果你
为您的文档包使用捆绑结构,您还可以使用
NSBundle 或 CFBundleRef 例程。使用束结构是
特别适合存储多个的文档
本地化。

另请注意,Apple 一直在电报中表示,它将最大限度地减少“路径”/NSString API 的使用,转而使用 URL API,尽管现有的路径 API 无疑将在更多主要操作系统版本中继续使用。

Yes, it's absolutely okay to have a resources only bundle. Some of the verbage that you quote pre-exists iOS. In OS X you can dynamically load executable code, that's specifically excluded in iOS.

Localization is an example of resource only bundles.


Edit:

The Bundle Programming Guide says:

Although document formats can leverage the bundle structure to
organize their contents, documents are generally not considered
bundles in the purest sense. A document that is implemented as a
directory and treated as an opaque type is considered to be a document
package, regardless of its internal format. For more information about
document packages, see “Document Packages.”

which says:

There are several ways to access the contents of a document package.
Because a document package is a directory, you can access the
document's contents using any appropriate file-system routines. If you
use a bundle structure for your document package, you can also use the
NSBundle or CFBundleRef routines. Use of a bundle structure is
especially appropriate for documents that store multiple
localizations.

also note that Apple has been telegraphing that it is minimizing the use of "path"/NSString APIs in favor of URL APIs, though existing path APIs will no doubt continue for many more major OS releases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文