Affiliate Disclosure: By buying the products we recommend, you help keep the lights on at MakeUseOf. Read more.
Swift is one of the hottest languages around right now, and for good reason. Mac and iOS apps take up a huge portion of the market. Being able to build iOS apps natively is a big deal for folks who don’t want to dip into the murky depths of Objective C.
Since Swift is native to Apple, you need a Mac, right? Wrong. While there is no “out of the box” method to compile Swift on Windows, that doesn’t mean that Windows users cannot learn Swift.
Here’s how to create a simple Swift program and compile and run it in Windows 10.
What Is Swift?
Before beginning, let’s look at what Swift actually is. Swift is a programming language designed by Apple. It takes ideas “from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list” according to project originator Chris Lattner.
It’s a relatively young language which was released to the public in 2014, though it is already widely regarded. The TIOBE Index of top programming languages in 2017 placed Swift at number 11, making it one of the fastest growing languages of all time.
In short, if you’re programming for Mac or iOS, Swift is for you! For a more in-depth look at the uses for Swift, take a look at these reasons why Swift is worth learning.
Getting Started With Swift on Windows 10
Firstly, we are going to need an editor to write our code in. You can use any IDE you are comfortable with, though it isn’t strictly necessary to use one and any text editor will also suffice. It mostly comes down to personal preference, though if you need help deciding what to use this guide may help you.
Today we are going to use Notepad++ as it is free, simple, and extensible. Download Notepad++ and open it up. Lets get down to some coding!
A Simple Swift for Windows Program
For our test project today we are going to create a simple program which will run on the Windows command line. Start by opening a new Notepad++ file. We’ll begin by printing a question to the screen, wait for the user to type their response, and then use this response to provide an answer.
print("What is your name?")
This will display when the program runs. Now that we have asked a question, we should provide a way to let the user answer. For this, we use the readline() method and store the answer as a variable called response.
var response = readLine()
Python users might already be more at home here!
Adding an Output
Now that we have this information stored in a variable, we want to use it and display it back to the user. What could be nicer than wishing them a great day?
print("Hello \(response!), I hope you are having a great day!")
Even if you have experience in other languages, you will see some differences here. Rather than using the + operator outside of the quotation marks to display your variable, you use \(variableName) within the quotation marks. Another feature of Swift is the use of Optional Values. These values are hard to understand at first glance, but add greater functionality to the use of variables within Swift.
In this instance, we simply want to display the value as it is, so we add an exclamation mark after the variable name response! to denote that it is not an Optional Value. An optional value is a variable that may or may not get assigned a value. It doesn’t require one. If it isn’t assigned a value, then it will be assigned nil.
A question mark (?) after the value type identifies it as optional, while an exclamation means that it isn’t.
Your code will look something like this:
To save your code, use File > Save As and select Swift file from the Save As Type menu. If your menu is missing the Swift file type, select all files instead, and add the .swift file extension after your chosen filename.
Compiling Swift in Windows 10
Now that we have a program, we need to be able to compile and run it. While there is no built in way to program Swift in Windows 10, there is a work around. Han Sangjin has created a compiler for Swift which is available for download from Github. Download and install the Swift for Windows application using the instructions provided in the link.
Once it is installed, open it up. Click on the Select File button and select your previously made program. Click Compile and wait for the program to compile.
For a program this tiny it should be nearly instant, though it can take time depending on how complex you made your code!
You should receive a “Successfully compiled” message in the dialog box. If not, go back and check through your code to make sure you haven’t made any errors. Once the code compile, click Run to run your program. The program will open in the Windows Command Line, and should look like this:
It is worth noting that you must use the Swift for Windows application to run your code, the EXE file that’s created will not work stand alone, even if the application is open.
Begin Coding Swift on Windows Today
If you decide that Swift is for you, there are a wealth of tools out there to help you. Once you’ve got a basic grasp of the environment, why not try a few beginner projects to bring your knowledge to life?
If you are a Linux user looking to code iOS apps, here’s how to code in Swift with Ubuntu.