How to Program in Swift in Windows 10
Whatsapp Pinterest
Advertisement

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 Characteristics of C Programming That Make It Unique (And Better) Characteristics of C Programming That Make It Unique (And Better) The C programming language is the mother of all modern programming languages. Almost every language in use today includes several features which first appeared in the C language. Read More too).

If you’re new to programming, you can certainly benefit from learning a newer language such as Python Try Python in Your Browser With These Free Online Interactive Shells Try Python in Your Browser With These Free Online Interactive Shells Whether you're going through these Python examples or reviewing the basics of arrays and lists, you can test the code right in your browser. Here are the best online Python interpreters we've found. Read More or Ruby The Best Interactive Introduction to Ruby for Newbies The Best Interactive Introduction to Ruby for Newbies If you're thinking of learning Ruby, here's a great baby step you can take. Read More . 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.

how to program swift in windows 10
Image Credit: stackify.com

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 7 Reasons the Swift Programming Language Is Worth Learning 7 Reasons the Swift Programming Language Is Worth Learning You ought to learn the Swift programming language sooner rather than later if you don't want to be left behind. Here are some excellent reasons that may convince you. Read More .

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 Text Editors vs. IDEs: Which One Is Better For Programmers? Text Editors vs. IDEs: Which One Is Better For Programmers? Choosing between an advanced IDE and a simpler text editor can be hard. We offer some insight to help you make that decision. Read More .

Today we are going to use Notepad++ as it is free, simple, and can be extended when required Soup Up The Notepad++ Text Editor With Plugins From The Notepad Plus Repository [Windows] Soup Up The Notepad++ Text Editor With Plugins From The Notepad Plus Repository [Windows] Notepad++ is a worthy replacement for Windows’ in-built and much loved text editor, and comes highly recommended if you’re a programmer, web designer or simply someone who finds themselves in need of a powerful plain... Read More . Download Notepad++ and open it up. Lets get down to some coding!

how to program swift in windows 10

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()

If you’re already familiar with other programming languages you may notice a few small differences here. Firstly, we can store the data acquired from readLine as a var instead of having to specify that it is going to be a string. Another change for those of you coming over from JavaScript Really Learn JavaScript with 5 Top Udemy Courses Really Learn JavaScript with 5 Top Udemy Courses JavaScript is the programming language of the web. If you have a reason to learn JavaScript, these five excellent courses from Udemy could be the place to begin your coding journey. Read More or C# The 17 Best YouTube Programming Tutorials The 17 Best YouTube Programming Tutorials In this post, we'd like to point you to some of the best YouTube programming series we've found. All of these are sufficient for getting your feet wet as a newbie programmer. Read More is the lack of semicolons to denote the end of lines. Python users 10 Basic Python Examples That Will Help You Learn Fast 10 Basic Python Examples That Will Help You Learn Fast This article of basic python examples is for those who already have some programming experience and simply want to transition to Python as quickly as possible. Read More might already be more at home here!

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:

how to program swift in windows 10

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:

how to program swift in windows 10

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:

how to program swift in windows 10

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.

Small Beginnings

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 7 Practical Reasons to Start Using a Virtual Machine 7 Practical Reasons to Start Using a Virtual Machine The term "virtual" has been co-opted in so many ways. Virtual reality. Virtual RAM. Virtual schools. But Virtualization in the context of PCs can be confusing. We're here to clear it up. Read More . 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 The Best Places to Learn Swift, Apple's Programming Language The Best Places to Learn Swift, Apple's Programming Language If you want to learn Swift, now is the time to dive in. The language has a bright future and the faster you learn it, the sooner you'll be able to reap the rewards. Read More out there to help you. Once you’ve got a basic grasp of the environment, why not try a few beginner projects So You Want To Make iPhone Apps? 10 Projects For Beginners So You Want To Make iPhone Apps? 10 Projects For Beginners Want to create iPhone and iPad apps? Start by learning the basics of Swift. Read More 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 How to Start Programming in Swift on Ubuntu How to Start Programming in Swift on Ubuntu Apple recently released their Swift programming language as open source, which means that anyone can now use this trending programming language, on any system, including Ubuntu! Read on to find out how to get started. Read More 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.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Mike Tungate
    March 20, 2018 at 2:32 pm

    Starting the Swift journey and trying to get it to function on Windows hasn't been straight forward.
    Thanks.