如何获取我的框架代码的包?
How can I obtain the bundle for my framework code?
有什么方法可以知道哪个包对应一个源代码文件?
我想要完成的是以下内容:
我有一个 source.swift/source.m 文件,在 Framework X 中。这个文件有一个函数,例如:如果它在 Swift -> whoIsMyEnclosingBundle() -> NSBundle
.
要使此功能正常工作,我需要能够在该框架内加载例如我的故事板。
我想让这个函数通用,所以不要硬编码包的名称
我想你正在寻找 +[NSBundle bundleForClass:]
。
如果您不在 class 内,则可以使用 +bundleWithIdentifier:
(这意味着您需要提前知道标识符)。文档建议为需要自己的包的框架使用该方法。
In Swift
There are several ways:
第一个是每次都有效的:
let bundle = NSBundle(forClass: [the class you are in].self)
The next two will only work in some cases:
- 对于未标记为 class
的函数
let bundle = NSBundle(forClass: self.dynamicType)
- 类 从 Objective-C
桥接
let bundle = NSBundle(forClass: [the class you are in].classForCoder())
有关 self.dynamicType
和 self
的详细解释,请进入 this answer:
有什么方法可以知道哪个包对应一个源代码文件?
我想要完成的是以下内容:
我有一个 source.swift/source.m 文件,在 Framework X 中。这个文件有一个函数,例如:如果它在 Swift -> whoIsMyEnclosingBundle() -> NSBundle
.
要使此功能正常工作,我需要能够在该框架内加载例如我的故事板。
我想让这个函数通用,所以不要硬编码包的名称
我想你正在寻找 +[NSBundle bundleForClass:]
。
如果您不在 class 内,则可以使用 +bundleWithIdentifier:
(这意味着您需要提前知道标识符)。文档建议为需要自己的包的框架使用该方法。
In Swift
There are several ways:
第一个是每次都有效的:
let bundle = NSBundle(forClass: [the class you are in].self)
The next two will only work in some cases:
- 对于未标记为 class
let bundle = NSBundle(forClass: self.dynamicType)
- 类 从 Objective-C
桥接let bundle = NSBundle(forClass: [the class you are in].classForCoder())
有关 self.dynamicType
和 self
的详细解释,请进入 this answer: