How was swift intended to be used?

Can Swift be used "alone" to make programs in such a way that c++ is used to make programs and run them using the command window, or was Swift intended to be used more for app development in conjunction with other supplementary languages?

I am trying to learn Swift and wanted to re-code many of my c++ programs over to Swift.

Thank you for helping me understand Swift better

You can compile command line tools written in Swift from the command line using the swiftc command.

For example, if you create a file called test.swift, containing:

print("Hello world!")

You can compile it and run the resulting executable like this:

$ swiftc test.swift
$ ./test
Hello world!

See swiftc --help for options.


There is also a REPL, which you invoke from the command line using the swift command:

$ swift
Welcome to Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.39.1). Type :help for assistance.
  1> 1 + 2
$R0: Int = 3
1 Like

Swift is meant as a general purpose language, so it can be used in things from phone apps to things deployed on cloud servers. It’s also meant for systems level stuff, although the support there still needs some improvement.

1 Like

Also, if you want to deploy your tools and libraries in both macOS and Linux (or other platforms) take a look to the Swift Package Manager.

As a personal note, I had a lot of Python scripts and some basic C libraries that I progressively moved to Swift. During this process I noticed how Swift is a really good language in helping you write clean reusable code. Some of my basic scripts even became complex libraries, used by both terminal tools and real apps. Also, I would encourage you to try your Swift projects on Linux (try with Docker), it is really pleasant to see the same code working great cross-platform! :slight_smile:

ok, sounds good. For App development (simple apps, one page - two page) what else do I need to learn?
I have a program in c++ that helps ID local trees in the area.
For example, say you find a red oak, but don't know that it's a red oak. Using the program it will help you ID the tree based on the questions asked.
I would like to convert this into an app 1. for personal use and 2. to learn to build my first working app.
Do I need to mess around with cocoa or can I get away with just Swift?
I know that it may vary for every app, but just as an overall yes or no, what would be the simplest way to make a one or two-page app that asks a few questions.

You need Cocoa for the gui and generally all macOS/iOS features. Swift is only the language.

You can write a command-line app, but that limits the devices it can run on. From a design perspective, the best thing to do is write the core logic which takes a series of answers and spits out the identification. Then, you can slap on whatever UI you want for obtaining the answers and displaying the identification. Then you can create more complex apps, or support a wider variety of platforms as your knowledge increases.