Reputation: 870
I have the following code :
var myBundle:NSBundle
myBundle = NSBundle.mainBundle() // what is the role of this line ??
if let path = myBundle.pathForResource("CellDescriptor", ofType: "plist"){
var cellDescriptors: NSMutableArray!
cellDescriptors = NSMutableArray(contentsOfFile: path)
}
Why am I using this line of code myBundle = NSBundle.mainBundle()
? I read that it is initialising myBundle
object. But what does that exactly mean?
Why cannot I call pathForResource
function without initialising myBundle
object?
Thank you.
Upvotes: 0
Views: 59
Reputation: 2413
Let's think about this line step by step:
myBundle = NSBundle.mainBundle()
You declared variable myBundle with type NSBundle. Now you need init this variable with proper instance object. you can use for example, constructor. But for some classes you have 'static constructor'. If you don't set variable the proper object - it have nil value by default, so you can not access instance method.
NSBundle.mainBundle()
- here is the call to mainBundle method of NSBundle class (static method) which returns you the proper instance (for this case - it's main bundle of your application. from the documentation:
The NSBundle object corresponding to the bundle directory that contains the current executable. This method may return a valid bundle object even for unbundled apps. It may also return nil if the bundle object could not be created, so always check the return value.
Hope this helps.
Upvotes: 2