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 place. Being able to build iOS apps natively is a big deal for folks who don't desire to dip into the murky depths of Objective C.

Since Swift is native to Apple, yous need a Mac, right? Wrong. While there is no "out of the box" method to compile Swift on Windows, that doesn't hateful that Windows users cannot learn Swift.

Hither's how to create a simple Swift program and compile and run it in Windows 10.

What Is Swift?

Earlier beginning, let'south await at what Swift really is. Swift is a programming linguistic communication designed by Apple. It takes ideas "from Objective-C, Rust, Haskell, Red, Python, C#, CLU, and far besides many others to list" according to project originator Chris Lattner.

It'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 time.

In short, if y'all're programming for Mac or iOS, Swift is for you lot! For a more than in-depth expect at the uses for Swift, take a look at these reasons why Swift is worth learning.

Getting Started With Swift on Windows ten

Firstly, we are going to demand an editor to write our code in. Yous tin use whatsoever IDE you are comfy with, though it isn't strictly necessary to use one and any text editor will also suffice. It generally comes downwards to personal preference, though if you demand assist deciding what to utilize this guide may help you.

Today we are going to utilize Notepad++ as it is free, elementary, and extensible. Download Notepad++ and open it up. Lets get downwardly to some coding!

Notepad++ is a highly capable code editor

A Simple Swift for Windows Program

For our test project today nosotros are going to create a simple program which volition 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 apply 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 utilize the readline() method and shop the answer every bit a variable called response.

          var response = readLine()        

If yous're already familiar with other programming languages you may notice a few small differences here. Firstly, we tin shop the data acquired from readLine every bit a var instead of having to specify that it is going to be a string. Some other change for those of you coming over from JavaScript or C# is the lack of semicolons to denote the terminate of lines.

Python users might already exist more at domicile hither!

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 exist nicer than wishing them a smashing 24-hour interval?

          print("Hullo \(response!), I hope you are having a great day!")        

Fifty-fifty if you take experience in other languages, yous will meet some differences hither. Rather than using the + operator outside of the quotation marks to brandish your variable, y'all utilise \(variableName) within the quotation marks. Another feature of Swift is the use of Optional Values. These values are hard to understand at starting time glance, but add together greater functionality to the use of variables within Swift.

In this example, we merely want to display the value as information technology is, so we add together an exclamation mark afterwards 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 crave one. If information technology isn't assigned a value, so it volition be assigned nada.

A question mark (?) after the value type identifies information technology as optional, while an exclamation means that it isn't.

Your code will expect something like this:

This is what your Swift code should look like

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

Compiling Swift in Windows ten

Now that we accept a plan, nosotros need to be able to compile and run it. While at that place is no built in way 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.

Once it is installed, open it upwards. Click on the Select File button and select your previously made program. Click Compile and await for the programme to compile.

Swift For Windows 2.0

For a program this tiny it should be well-nigh instant, though it tin can take time depending on how complex you made your code!

Y'all should receive a "Successfully compiled" message in the dialog box. If non, go back and bank check through your code to make sure you haven't made any errors. Once the lawmaking compile, click Run to run your programme. The program will open up in the Windows Command Line, and should look like this:

The output from your simple Swift program

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

Brainstorm Coding Swift on Windows Today

If you decide that Swift is for you lot, in that location are a wealth of tools out in that location to help you. Once you've got a bones grasp of the environment, why non try a few beginner projects to bring your knowledge to life?

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

The 20 Best Android Television set Apps Worth Installing ASAP

Read Adjacent

About The Author