[NSBundle bundleForClass:[self class]]]是什么意思? [英] [NSBundle bundleForClass:[self class]]] what does that mean?

查看:754
本文介绍了[NSBundle bundleForClass:[self class]]]是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在创建cocoapod库。如果要加载json,png和其他xib文件,则需要使用 [NSBundle bundleForClass:[self class]]] 而不是使用主捆绑包。我想知道这样做的推荐方法是什么?

I am now creating cocoapod library. If I want to load json,png and other xib files, I need to use [NSBundle bundleForClass:[self class]]] instead of using main bundle. I wanted to know is this recommend way to do like this and what does that mean?

推荐答案


捆绑软件是macOS和iOS中的一项基本技术,用于
封装代码和资源。捆绑包通过提供所需资源的已知位置来简化开发人员
的体验,而
则减轻了创建复合二进制文件的需要。相反,捆绑包
使用目录和文件来提供更自然的
组织类型,也可以在
开发期间和部署之后轻松地对其进行修改。

Bundles are a fundamental technology in macOS and iOS that are used to encapsulate code and resources. Bundles simplify the developer experience by providing known locations for needed resources while alleviating the need to create compound binary files. Instead, bundles use directories and files to provide a more natural type of organization—one that can also be modified easily both during development and after deployment.

一个Xcode项目可以包含多个目标,而这些目标本身可能包含不同的文件(例如资产文件或某些源/配置等)。

An Xcode Project can contain multiple Targets, which itself may contain different files (e.g. asset files or some sources/configs etc.).

这样,您不必将所有文件打包在所有目标中,但是可以区分。
特别是对于测试这是一种很好的方法,因为您可以提供某些测试文件/资源​​/无效文件来测试您的代码。
因为您确实测试了mainBundle中的大部分时间代码(基本上将提供您的应用程序),但同时也使用了资产或其他不应在您的应用程序中打包的东西。

This way you don't have to pack ALL files in ALL targets, but can differentiate. In particular for testing this is a great approach, because you can provide certain testing files/resources/"invalid" files to test your code. Because you do test most of the time code that is in your mainBundle (basically your application, that will be provided) but use also assets or other stuff that shall not be packet within your application.

如果要使用此功能(您可能会迷失了!),则需要从其他包中加载某个文件。

If you want to (and you probably will stumble over this!) make use of this functionality, you will want to load a certain file from a different bundle.

因此,您将无法使用

[[NSBundle mainBundle] pathForResource:@"tests" ofType:@"plist"]

但是将在您的Tests捆绑包中使用XCTTestCase-Class:

But will use IN your XCTTestCase-Class in your Tests bundle:

// https://developer.apple.com/reference/foundation/bundle
[[NSBundle bundleForClass:self.class] pathForResource:@"tests" ofType:@"plist"]

https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/AccessingaBundlesContents/AccessingaBundlesContents.html

如果如果您真的想进一步了解Bundle,请参阅以下链接:
https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html#//apple_ref/doc / uid / 10000123i

If you really want to know more about Bundles, see this link: https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html#//apple_ref/doc/uid/10000123i

它们非常强大且有用,所以请花点时间:-)

They are very powerful and useful, so take your time :-)

这篇关于[NSBundle bundleForClass:[self class]]]是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆