0

Is there any way to know which bundle corresponds to a source code file?

What I'm trying to accomplish is the following:

I have a source.swift/source.m file, inside of a Framework X. This file has a function, for instance: if it were in Swift -> whoIsMyEnclosingBundle() -> NSBundle.

What I need for this function to work properly is for me to be able to load, for instance, my Storyboards inside that framework.

I would like to make this function general, so NO hardcoding of the bundle's name

jscs
  • 63,694
  • 13
  • 151
  • 195
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65

2 Answers2

1

I think you're looking for +[NSBundle bundleForClass:].

If you're not inside a class, then you could use +bundleWithIdentifier: (this does mean you need to know the identifier ahead of time). The documentation recommends that method for frameworks that need their own bundle.

jscs
  • 63,694
  • 13
  • 151
  • 195
0

In Swift

There are several ways:

The first one is the one that will work everytime:

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

The next two will only work in some cases:

- for functions not marked as class

let bundle = NSBundle(forClass: self.dynamicType)

- for classes bridged from Objective-C

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

For a great explanation on self.dynamicType and self go into this answer:

Community
  • 1
  • 1
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65