Cocoa - 从非主包加载资源
我的应用程序加载一些自定义捆绑包,并且它想要使用这些捆绑包中的资源。
似乎不可能从主包实例获取资源,例如 [NSBundle mainBundle] pathForResource:ofType:]。
所以我用 +[NSBundle pathForResource:ofType:inDirectory:] 替换它,效果很好。但是资源加载代码应该知道“inDirectory:”的捆绑路径,这非常不方便。如果捆绑包移动到其他路径,它将不起作用。
还有其他方法可以解决这个问题吗?谢谢你!
My application loads some custom bundles and it wants to use resources in these bundles.
It seems that it's impossible to get resources from main bundle instance, like [NSBundle mainBundle] pathForResource:ofType:].
So I replace it with +[NSBundle pathForResource:ofType:inDirectory:] and it works well. But it's quite inconvenient that the resource-loading code should know the bundle path for "inDirectory:". It won't work if bundles are moved to another paths.
Are there other methods to solve this? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不完全确定您想要完成什么,但是如果您想加载捆绑包资源而不使用捆绑包的直接路径,您可以使用
[NSBundle bundleForClass:]
或 <代码> [NSBundle bundleWithIdentifier:] 。一旦您引用了所需的捆绑包实例,您就可以按如下方式访问其内容:如果您希望即使在捆绑包移动后它仍然准确,这可能是一个更棘手的问题。正如文档所述,“NSBundle 对象代表文件系统中的一个位置......”。如果该位置发生移动,捆绑包实例仍将在现有位置中查找。我认为您需要找出另一种机制来确定捆绑包是否已移动并从新位置重新创建 NSBundle 实例。
I'm not totally sure what you're trying to accomplish, but if you want to load a bundle resource without using the direct path to the bundle, you can use either
[NSBundle bundleForClass:]
or[NSBundle bundleWithIdentifier:]
. Once you have a reference to the instance of the bundle you want, you can access its contents as follows:If you want it to still be accurate even after the bundle has moved, that might be a trickier question. As the documentation states, "An NSBundle object represents a location in the file system...". If that location moves, the bundle instance will still look in the existing location. I think you will need to figure out another mechanism to determine if the bundle is moved and recreate your NSBundle instance from the new location.