Swift is one of the hottest languages around right now, and it’s made a lot of people excited, for good reason. OS X 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 (although there are many great reasons to learn C too).
If you’re new to programming, you can certainly benefit from learning a newer language such as Python or Ruby. Swift fits into this box as well, cutting out some corners which some believe to be a hindrance for a beginner learning to code.
Since Swift is native to Apple, it might seem hard to know where to begin if you don’t own a Mac. There is no “out of the box” way to compile Swift on Windows. Does this mean that Windows users cannot learn Swift? As it turns out, no! There is a way.
This article will guide you through creating a simple Swift program, and how to compile and run it in Windows 10.
What Is Swift?
Before we get started, let’s look at what Swift actually is. Swift is a programming language designed by Apple, which 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 OS X 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 in Windows
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.
A Swift Hello to You!
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 as soon as the program is run. 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()
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 are experienced 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.
Now that we are done, your code should 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. You will be presented with a UI like this:
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 is compiled, 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.
Since Swift is not designed for use within Windows 10, this is the only way to get started with the language at present outside of running a virtual machine. While it is limited, it is a great way for those curious about the language to get hands on experience with it and learn it’s syntax.
If you decide that Swift is for you and plan to work with it on OS X, 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 and are wondering whether it’s possible to code in Swift using Ubuntu, we’ve got some great news for you!
Are you learning Swift? How do you feel it compares to other new coding languages? Have we missed any other great hacks to get started with it on Windows 10? Let us know in the comments section below!
Explore more about: Swift.