How to use the Swift programming language in Windows 10

Swift is one of the hottest languages ​​today and it is developed by Apple so users cannot compile Swift on a Windows computer. So this article will show you how to use the Swift programming language on Windows 10.

Swift is one of the hottest languages ​​today, and it makes many people happy. OS X and iOS apps make up a large part of the market, so being able to build iOS apps naturally is important for people who don't want to use Objective C (although there are many great reasons). to learn programming language C).

If you are new to programming, you will certainly benefit from learning a newer language like Python or Ruby. Swift is the same, it breaks the notion of some people who think that this language will be an obstacle for new learners.

Swift originated from Apple, it seems hard to know where to start if you don't own a Mac. There is no way to compile Swift on Windows. Does this mean that Windows users cannot learn Swift? However, there is a way to use Swift on Windows. This article will guide you to create a simple Swift program, and how to compile and run it on Windows 10.

What is Swift?

Before we start, let's see what Swift really is. Swift is an Apple-designed programming language. According to the Chris Lattner project initiator, it got ideas "from Objective-C, Rust, Haskell, Ruby, Python, C #, CLU, and other programming languages".

This is a relatively young language widely published in 2014, although it has been widely known before. The TIOBE Index released the top programming language list in 2017, in which Swift ranked at the 11th position, making it one of the fastest growing languages ​​of all time.

  1. What is the Swift programming language? What does it mean for mobile apps?

Picture 1 of How to use the Swift programming language in Windows 10

In short, if you're programming for OS X or iOS, Swift is the right programming language for you. For a more detailed look at how to use Swift, see the reasons why Swift should be learned.

Use Swift in Windows

First, we will need an editor to write code. You can use any IDE to feel comfortable, although not necessarily using the IDE, you can use a text editor. Users should use Notepad ++ because it is free, simple, and can be expanded as needed. Download Notepad ++ if not available and open it.

Picture 2 of How to use the Swift programming language in Windows 10

In this article, we will create a simple program that runs on the Windows command line. Start by opening a new Notepad ++ file. We will start by "printing" a question into the screen, waiting for the user to type their feedback, then use this feedback to provide the answer.

 print("What is your name?") 

This question will show as soon as the program runs. We have now asked a question, then provide a way to allow users to respond. To do this, use the readline () method and save the answer as a variable called response .

 var response = readLine() 

If you're familiar with other programming languages, you may notice a few minor differences here. First, you can save the data obtained from readLine as var instead of having to specify in a string. Another change for JavaScript or C # users is the lack of commas to indicate the end of the lines. Python users may feel more familiar.

Now that this information is stored in a variable, you will use it and show it back to the user.

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

Even if you have experience in using other languages, you will see some differences here. Instead of using the + operator outside the quotes to display the variable, here use (variableName) inside the quotation marks. Another Swift feature is using Optional Values . Although users will initially find these values ​​difficult to understand, you can use more functions with these variables.

In this case, we just want to display its value, so you'll add an exclamation mark after the variable name response! to indicate that it is not a Optional Value . Optional value is a variable that can or cannot be assigned to a value (it does not need a value). If it is not assigned a value, it will be zero. A question mark (?) After the value type, specifying it as an option, while an exclamation point means that it is not an optional value.

Once done, the code will look like this:

Picture 3 of How to use the Swift programming language in Windows 10

To save the code, use File> Save As and select Swift file from the Save As Type menu. If the menu does not have a Swift file type, select all files and add the .swift file extension after the selected file name.

Compile Swift in Windows 10

Now that we have a program, you need to compile and run it. Although there is no way to program Swift in Windows 10, there is another alternative. Han Sangjin created a compiler for Swift available for download from Github. Download and install the Swift for Windows application using the instructions provided in the link. Once installed, open it, you will see the following interface:

Picture 4 of How to use the Swift programming language in Windows 10

Click the Select File button and select the program you created earlier. Click Compile and wait for the program to compile. For a small program like this, you will get instant results, different compile times depending on your code.

The user will receive a " Successfully compiled " message in the dialog box. If you don't see this message, go back and check the code to make sure there aren't any errors. After the code is compiled, click Run to run the program. The program will open in the Windows Command Line and will look like this:

Picture 5 of How to use the Swift programming language in Windows 10

Note that you must use the Swift for Windows application to run the code, the generated .EXE file will not work independently, even if the application is open.

Because Swift is not designed for use in Windows 10, this is the only way to use the current language in addition to running a virtual machine. Although it's limited, it's a great way for people who are curious about the language to gain experience and learn the syntax.

If you decide to use Swift and plan to work with it on OS X, there are many tools that can help you. Once you have mastered the basic environmental skills, why not try a few new projects to gain more knowledge.

See more:

  1. Learning one of these 16 programming languages ​​helps you not to be unemployed
  2. 7 Framework JavaScript for mobile application development
  3. 5 free application building platforms do not need code
Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile