0

I am reading a book related to IOS development. And i am facing a problem when i reading this line let bundle = NSBundle.mainBundle(). The book told that

this call returns a bundle object that represents our application.

I know, in swift an object is created as let objname = classname() . Please tell how let bundle = NSBundle.mainBundle() create an object?

Alamin
  • 877
  • 2
  • 12
  • 17

2 Answers2

3

This call does not create (instanciate) a new object. It calls the mainBundle class method on the NSBundle class, which returns a singleton, the main bundle of your app.

Similar calls (in obj-C, but translatable in Swift easily):

  • [UIApplication sharedApplication]
  • [NSNotificationCenter defautCenter]
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • So an object is created at the first call: The singleton object. However, one should not care about that. – Amin Negm-Awad Jun 25 '15 at 11:18
  • Where the bundle object is created? @Cyrille – Alamin Jun 25 '15 at 17:30
  • It's created by the objective-C runtime and by iOS as part of launching your application, along the UIApplication shared instance. You have absolutely no control over it and don't need to worry, know or care about where it comes from. Principle of least responsibility. – Cyrille Jun 25 '15 at 17:35
0

Every method returning an object at all can return a reference to a new object or a reference to an existing one. How could one prohibit one of that options? And how could one say that without knowing the implementation?

Simply accept that an object is returned. Period.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50