Cocoa - 从非主包加载资源

发布于 2024-12-26 08:03:15 字数 274 浏览 0 评论 0原文

我的应用程序加载一些自定义捆绑包,并且它想要使用这些捆绑包中的资源。

似乎不可能从主包实例获取资源,例如 [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 技术交流群。

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

发布评论

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

评论(1

断念 2025-01-02 08:03:15

我不完全确定您想要完成什么,但是如果您想加载捆绑包资源而不使用捆绑包的直接路径,您可以使用 [NSBundle bundleForClass:] 或 <代码> [NSBundle bundleWithIdentifier:] 。一旦您引用了所需的捆绑包实例,您就可以按如下方式访问其内容:

NSBundle myBundle = [NSBundle bundleWithIdentifier:@"com.my.identifier"];
[myBundle pathForResource:@"myResource" ofType:@"type"];

如果您希望即使在捆绑包移动后它仍然准确,这可能是一个更棘手的问题。正如文档所述,“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:

NSBundle myBundle = [NSBundle bundleWithIdentifier:@"com.my.identifier"];
[myBundle pathForResource:@"myResource" ofType:@"type"];

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.

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