Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode 6 missing main.swift file for new projects

Tags:

xcode

macos

swift

I reinstalled Xcode 6 after not being able to get the usual ctrl-click help. Now, whenever I create a new Xcode project for OS X Cocoa Application, the main.swift file is no longer in the supporting files as it used to be. I have 2 partitions and Mavericks on one, Yosemite on the other. Can anybody please tell me what is going on and how to get around it? This is my first question, be free to point me out any mistakes in it as well, please. Thanks

like image 223
Artur Pinheiro Avatar asked Dec 05 '22 05:12

Artur Pinheiro


1 Answers

You don't necessarily need a main.swift file. The main (heh) purpose of that file is to call NSApplicationMain(), the function that gets a Cocoa app up and running. But, since every app needs to do that, Apple added a less-boilerplate way of doing things.

Now, you just need to label your app delegate class with the @NSApplicationMain attribute. Then Xcode will build in the right hooks to get your app launched into its main run loop with an instance of your chosen class as NSApp's delegate. The new project templates do this, which is why you don't need a main.swift anymore. (It also frees you from needing to designate the app delegate in a main nib, which is useful for apps using OS X storyboards.)

Of course, if you want to customize your app setup a bit more—say, by running some code before the run loop kicks off, or with a custom NSApplication subclass—you can still create a main.swift file and call NSApplicationMain() yourself. (If you do, be sure to also remove the @NSApplicationMain attribute from your app delegate class.)

like image 179
rickster Avatar answered Dec 20 '22 22:12

rickster