How mainBundle() return on object in swift or objective-c?

224 views Asked by At

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?

2

There are 2 answers

3
Cyrille On

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]
0
Amin Negm-Awad On

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.