Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write simple Xcode Swift program without a UI

I'm trying to get familiar with Xcode and Swift by writing a simple hello world program that has no UI.

When I create a new empty project in Xcode, there is no Target (scheme?), so I can't build and run. How can I create a target/scheme to satisfy the minimal build requirements? Steps to reproduce:

  1. Xcode > New > Project > Other > Empty
  2. code = print ("TEST")
  3. build/run disabled

Alternatively, when I create a new App project it has UI that I can't seem to ignore. Where can I insert a function and print statement without getting a compile time error?
Steps to reproduce:

  1. Xcode > New Project > Mac/iOS/TVos/watchOS > App
  2. Put a print statement virtually anywhere in any file gives a compiler error
like image 202
torpedo51 Avatar asked Aug 26 '26 22:08

torpedo51


2 Answers

If all you need is just writing a simple swift program, then you can create a swift file as simple as below:

test.swift

print("hello world")

Then run swift test.swift in your terminal.

If you want to use Xcode, then you can create a simple command-line app by selection: Xcode -> File -> Project -> mac OS -> Command Line Tool. Then Xcode will create a project with a swift file call main.swift which contains a simple print statement. Just build and run your project and check the console out.

like image 95
congnd Avatar answered Aug 28 '26 14:08

congnd


If you just code without UI you can use Playground:

Xcode -> File -> New -> Playground
like image 36
aturan23 Avatar answered Aug 28 '26 14:08

aturan23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!