Swift is 1 of the hottest languages effectually right at present, and for good reason. Mac and iOS apps accept up a huge portion of the market. Being able to build iOS apps natively is a large deal for folks who don't want to dip into the murky depths of Objective C.

Since Swift is native to Apple, you lot 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'south how to create a simple Swift programme and compile and run it in Windows x.

What Is Swift?

Before first, let'southward look at what Swift really is. Swift is a programming linguistic communication designed by Apple. Information technology takes ideas "from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far besides many others to list" according to project originator Chris Lattner.

Information technology's a relatively young linguistic communication which was released to the public in 2014, though information technology 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 fourth dimension.

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, accept 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 lawmaking in. You lot can use whatsoever IDE yous are comfortable with, though it isn't strictly necessary to use one and any text editor will also suffice. It mostly comes downward to personal preference, though if you lot need assist deciding what to use this guide may help yous.

Today we are going to use Notepad++ as information technology is gratuitous, simple, and extensible. Download Notepad++ and open it up. Lets get down to some coding!

Notepad++ is a highly capable code editor

A Simple Swift for Windows Programme

For our test project today nosotros 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 past printing a question to the screen, wait for the user to type their response, and so use this response to provide an answer.

                      print("What is your name?")        

This will display when the programme runs. Now that we take asked a question, we should provide a way to allow the user reply. For this, we use the readline() method and shop the answer equally a variable called response.

                      var            response = readLine()        

If you're already familiar with other programming languages you may find a few small differences here. Firstly, we can store the data caused from readLine as a var instead of having to specify that information technology is going to exist a string. Some other change for those of yous coming over from JavaScript or C# is the lack of semicolons to announce the cease of lines.

Python users might already be more at home here!

Adding an Output

At present that nosotros have this information stored in a variable, nosotros want to use it and brandish it back to the user. What could be nicer than wishing them a groovy day?

                      impress("Hello \(response!), I hope you are having a great mean solar day!")        

Even if yous accept experience in other languages, you volition see some differences here. Rather than using the + operator outside of the quotation marks to brandish your variable, yous use \(variableName) inside the quotation marks. Some other feature of Swift is the use of Optional Values. These values are hard to understand at beginning glance, but add together greater functionality to the use of variables within Swift.

In this instance, we simply desire to display the value every bit it is, so we add together an assertion marker later on the variable name response! to announce that it is not an Optional Value. An optional value is a variable that may or may not become assigned a value. It doesn't require one. If it isn't assigned a value, then information technology will be assigned nil.

A question mark (?) later the value blazon identifies it every bit optional, while an assertion means that it isn't.

Your lawmaking will look something like this:

This is what your Swift code should look like

To save your code, use File > Salve As and select Swift file from the Save As Type menu. If your bill of fare is missing the Swift file type, select all files instead, and add the .swift file extension after your chosen filename.

Compiling Swift in Windows x

At present that we have a program, nosotros need to be able to compile and run information technology. While there is no congenital in mode to program Swift in Windows 10, at that place 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.

One time it is installed, open it upwards. Click on the Select File push and select your previously made program. Click Compile and wait for the program to compile.

Swift For Windows 2.0

For a program this tiny it should exist near instant, though it tin can have time depending on how complex you fabricated your lawmaking!

Y'all should receive a "Successfully compiled" message in the dialog box. If non, go back and check through your lawmaking to brand sure y'all 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 expect similar this:

The output from your simple Swift program

It is worth noting that y'all must use the Swift for Windows application to run your code, the EXE file that'south created will not work stand solitary, even if the application is open up.

Brainstorm Coding Swift on Windows Today

If you decide that Swift is for yous, at that place are a wealth of tools out there to help you. Once you lot've got a basic grasp of the environment, why not endeavor a few beginner projects to bring your noesis to life?

If you are a Linux user looking to code iOS apps, here's how to lawmaking in Swift with Ubuntu.

The 20 All-time Android TV Apps Worth Installing ASAP

Read Side by side

About The Author