49026

What the meaning of [NSBundle mainBundle] in iPhone?

Question:

RootViewController *rvController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

sometimes i see it use nil ,i dont know what exactly it use for?

Answer1:

Here is the exact explanation for that:

<blockquote>

The bundle in which to search for the nib file. This method looks for the nib file in the bundle's language-specific project directories first, followed by the Resources directory. If nil, this method looks for the nib file in the main bundle.

</blockquote>

I think [NSBundle mainBundle] doesn't do anything different than you put nil. However, if you want to do internationalization and localization, you can use them

<a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:" rel="nofollow">More details here</a>

Answer2:

<a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/doc/uid/20000214-BCIDFECI" rel="nofollow">http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/doc/uid/20000214-BCIDFECI</a>

<blockquote>

This method allocates and initializes a bundle object if one doesn’t already exist. The new object corresponds to the directory where the application executable is located. Be sure to check the return value to make sure you have a valid bundle. This method may return a valid bundle object even for unbundled applications.

In general, the main bundle corresponds to an application file package or application wrapper: a directory that bears the name of the application and is marked by a “.app” extension.

</blockquote>

According to the docs, initWithNibName:bundle: just calls this when you pass in nil.

<a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW15" rel="nofollow">http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW15</a>

<blockquote>

The bundle in which to search for the nib file. This method looks for the nib file in the bundle's language-specific project directories first, followed by the Resources directory. If nil, this method looks for the nib file in the main bundle.

</blockquote>

So, they are equivalent

Recommend

  • Find out default language on Linux
  • what is wrong with this code in settings.py file of my django application?
  • Getting Suggestions while installing Laravel through Composer
  • Drupal: multilanguage.. looking for the right module to build multiple versions of the node
  • add a custom base class for web forms
  • django and riak engine not found
  • localizable.strings - works in simulator but not on device
  • haml, link_to helper, and I18n in the same sentence
  • How to use get all NSLocalization using genstrings while preserved current translations
  • Wordpress plugin for dependent drop down lists
  • What is wrong in my MVC implementation?
  • SSRS 2008 - Sorting within a group
  • How do I create an image and save it for later to draw as texture in XNA?
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Where these are stored?
  • Yii2: Finding file and getting path in a directory tree
  • Bash if statement with multiple conditions
  • Implicit joins and Where in Doctrine - how?
  • AndEngine Applying Transparancy to AndEngine View
  • Web.config system.webserver errors
  • Apache RewriteRule redirection with url encoded
  • JBoss External Properties Files in Classpath
  • Parsing a CSV string while ignoring commas inside the individual columns
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to attach a node.js readable stream to a Sendgrid email?
  • Checking free space on FTP server
  • Change Inet root folder for iis 7
  • Java: can you cast Class into a specific interface?
  • Master page gives error
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Updated Ionic CLI but shows previous version (Windows)
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • bootstrap to use multiple ng-app
  • Matrix multiplication with MKL
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy