Search code examples
objective-cswiftcocoaxibnib

How does NSApplicationMain find the main nib file?


The documentation for the NSApplicationMain function says that it

loads the main nib file from the application’s main bundle

It's suggested on the web that NSApplicationMain finds "the main nib file" by looking at the Info.plist file at the key NSMainNibFile (obfuscated by XCode as "Main nib file base name"), i.e. NSApplicationMain does something like

let mainBundle: Bundle = Bundle.main
let mainNibFileBaseName: String = mainBundle.infoDictionary!["NSMainNibFile"] as! String
mainBundle.loadNibNamed(mainNibFileBaseName, owner: myApp, topLevelObjects: nil)

But if I delete the NSMainNibFile key, the application still finds and loads my MainMenu.xib file! And then if I rename the file to Foo.xib, the application instead finds and loads Foo.xib!

What is the real search procedure that NSApplicationMain uses to find "the main nib file"?


Solution

  • If the key NSMainNibFile is missing, NSApplicationMain calls [NSBundle loadNibNamed:nil owner:application] which calls [mainBundle pathForResource:nil ofType:@"nib"] which returns the first nib file it finds.