【问题标题】:How can I obtain the bundle for my framework code?如何获取框架代码的捆绑包?
【发布时间】:2016-03-11 22:49:52
【问题描述】:

有没有办法知道哪个bundle对应一个源代码文件?

我想要完成的是:

我在 Framework X 中有一个 source.swift/source.m 文件。这个文件有一个函数,例如:如果它在 Swift -> whoIsMyEnclosingBundle() -> NSBundle 中。

要使此功能正常工作,我需要能够在该框架内加载,例如,我的故事板。

我想让这个函数通用,所以捆绑包的名称没有硬编码

【问题讨论】:

    标签: ios cocoa-touch nsbundle


    【解决方案1】:

    我想你正在寻找+[NSBundle bundleForClass:]

    如果您不在班级内,则可以使用+bundleWithIdentifier:(这确实意味着您需要提前知道标识符)。该文档建议为需要自己的包的框架使用该方法。

    【讨论】:

    • 在 Swift 2.1 中是 NSBundle(forClass: self.dynamicType)
    【解决方案2】:

    在 Swift 中

    有几种方法:

    第一个是每次都可以使用的:

    • let bundle = NSBundle(forClass: [the class you are in].self)

    接下来的两个只在某些情况下有效:

    - 对于未标记为 class

    的函数
    let bundle = NSBundle(forClass: self.dynamicType)
    

    - 用于从 Objective-C 桥接的类

    let bundle = NSBundle(forClass: [the class you are in].classForCoder())
    

    有关self.dynamicTypeself 的详细说明,请访问this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 2012-10-22
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      相关资源
      最近更新 更多