2017-03-19 24 views
0

The documentation for the NSApplicationMain function說,它NSApplicationMain如何找到主要的筆尖文件?

負載從應用程序的主束主要筆尖文件

它建議NSApplicationMain通過查看Info.plist文件在發現「的主要筆尖文件」在網絡上關鍵NSMainNibFile(由的XCode混淆爲「主筆尖文件基本名稱」),即NSApplicationMain確實像

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

但是,如果我刪除NSMainNibFile密鑰,應用程序仍會找到並加載我的MainMenu.xib文件!然後,如果我將文件重命名爲Foo.xib,則應用程序會查找並加載Foo.xib

NSApplicationMain用來查找「主筆尖文件」的真實搜索程序是什麼?

回答

2

如果密鑰NSMainNibFile丟失,NSApplicationMain電話[NSBundle loadNibNamed:nil owner:application]這就要求[mainBundle pathForResource:nil ofType:@"nib"]返回找到的第一個筆尖文件。

+0

非常簡明明確的答案,謝謝!作爲參考:['load Nib命名(_:owner:options:)'](https://developer.apple.com/reference/foundation/bundle/1618147-loadnibnamed)和['path(for資源:類型:)'](https://developer.apple.com/reference/foundation/bundle/1410989-path) – jameshfisher

+0

我有點困惑,因爲'加載Nib命名(_: owner:options:)'不允許'nil'作爲第一個參數。此行爲是否記錄在案? – jameshfisher

+0

不,但Objective-C不介意。 [loadNibNamed:owner:](https://developer.apple.com/reference/foundation/nsbundle/1402904-loadnibnamed)已棄用。 – Willeke