In 2018, we wrote a post about using AppKit from the command line. We can use exactly the same technique to run a SwiftUI app directly from a Swift file.


When you’re writing a quick tool, perhaps to visualize something, or to get user feedback, an Xcode project can feel like overkill. It’s often very useful to have a single file that you just run with swift myfile.swift from Terminal. Likewise, when you’re inside a Swift Package Manager project, you might not want to create an additional Xcode project when you’re just testing things out.
What if you could just do the following?
Fortunately, you can, by pasting in the boilerplate code from this gist. Simply run swift boilerplate.swift in Terminal and you’ll have a macOS app up and running.
If you’re building a complex app, you’ll almost certainly want to move to a “real” application, but for one-off tools and prototyping we’ve found this shortcut very useful.
Screenshot
In Swift Talk Episode #145, which is free to watch, we use the same technique to create an AppKit app using Swift Package Manager.
We start building our Markdown Playgrounds app from a plain command-line app package, leveraging AppKit’s document architecture.
Episode 145 · March 29, 2019
7 Episodes · 2h55min
Stay up-to-date with our newsletter or follow us on Twitter.
Nov 27th, 2020
30% on everything until Monday
Nov 23rd, 2020
How grids lay out their content.
Nov 9th, 2020
Flexibility Defined
Browse the Archive
objc.io publishes books, videos, and articles on advanced techniques for iOS and macOS development.
Learn
Follow
More

source