纯资源 NSBundle:这是犹太洁食吗?
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/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 aContents/
directory or aContents/Info.plist
file, so it may not behave properly when treated as one. Proceed with caution.是的,拥有纯资源包绝对没问题。您引用的一些措辞早在 iOS 中就已存在。在 OS X 中,您可以动态加载可执行代码,这在 iOS 中是明确排除的。
本地化是纯资源捆绑的一个示例。
编辑:
捆绑包编程指南说:
其中说:
另请注意,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:
which says:
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.