How to work with the Swift Programming Language in Windows

How to work with the Swift Programming Language in Windows

To be honest, although may not want to work totally with this language on this environment to create applications or something like that, you may find very useful the fact of programming in this language in a Windows environment just for learning purposes of the language itself and its syntax. It's worth to mention that not everyone can afford the purchase of an apple device to work with this language, so if you are one of those that work in a windows environment and you may like to introduce yourself to the Swift Language, then you have found the correct article to get started with.

The Swift Language is a high-performance system programming language with a very clean and modern syntax, that offers seamless access to existing C and Objective-C code and frameworks, being memory safe by default. Although inspired by Objective-C and many other languages, Swift is not itself a C-derived language. As a complete and independent language, Swift packages core features like flow control, data structures, and functions, with high-level constructs like objects, protocols, closures, and generics. Swift embraces modules, eliminating the need for headers and the code duplication they entail. The open source SwiftForWindows project provides an easy-to-use development environment for Swift programming for Windows applications. The project works with the Swift compiler for Cygwin, MSVC, MinGW.

In this article, we'll show you an easy way to get started with the programming with the Swift Language in a Windows Environment.

1. Download SwiftForWindows compiler

As first step, you need to download the installer of Swift For Windows from the releases page of the project at Github here. The installer file size is around 110MB and you will be the one who decides where to install it. By default, it's installation path is at C:/Swift. Now you need to understand how the project works, after installing Swift For Windows, you will basically have at your disposition an executable that works as a compiler, it offers a graphical user interface that allows you to select the swift source file that you want to compile:

Swift Compiler Windows

You need to compile the file before running it, so if your code is valid and you click on Compile, you will be able to run it with the Run button, which normally will open a command prompt with the output of your code if there's any of course. For more information about this project, please visit the official repository at Github here.

2. Testing the compiler

To get started with the compiler, let's write some code ! Create a file somewhere on your system namely hello.swift. This file will have the following code inside:

print("Hello World !")

To define the file that the compiler should compile, click on Select File. This will open the system filebrowser and will allow you to select a file, select the file we've just created. The steps to run your Swift code on this environment is the following:

  1. Click on Compile
  2. If the compilation succeeds, click on Run

With the code that we wrote on our hello.swift file, the output will be:

Hello World On Swift in Windows

Note that while the command prompt with the output of your Swift Code is opened, the compiler tool will be blocked, so be sure to close the cmd window before trying to compile again. However, not everything in the life is printing hello world on every new programming language that you learn! You may want to get started with more complex code, for example the fibonacci series. Create a new file namely fibonacci.swift and paste the following code:

class FibIterator : IteratorProtocol {
    var (a, b) = (0, 1)

    func next() -> Int? {
        (a, b) = (b, a + b)
        return a
    }
}

let fibs = AnySequence{FibIterator()}

print(Array(fibs.prefix(10)))

Compile it and run it. As expected, the output will be:

Fibonacci Swift Output

About the logs

During the compilation, the compiler will have a log that is displayed directly in the application. This log contains the warnings, notes or errors of the code that is being compiled. You will not always get a Successfully compiled message in the output, so you will need to pay attention to the other kind of messages in the log. Note that once you Run the compiled code (which means your application compiled succesfully, as long as there's not an older version of your program), the log will be removed, so check it immediately after trying to compile. As on every sane compiler, you will get the line and character where the exception/warning occurs.

Errors

The compiler will be able to deal with errors during the compilation, for example with the following code that should display the date in the output:

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)

let year =  components.year
let month = components.month
let day = components.day

print(year)
print(month)
print(day)

Will throw the following errors (error: use of unresolved identifier 'Calendar' and 'Date'):

Swift Compiler Error Message

This error in our code is cause by the missing import statement at the beginning of our code 'import Foundation', the functional snippet is shown in the warnings area.

Warnings

The compiler will display warnings as well, if we fix our previous snippet importing the Foundation namespace, our code will compile succesfully:

import Foundation

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)

let year =  components.year
let month = components.month
let day = components.day

print(year)
print(month)
print(day)

However, we'll get some warnings and notes:

Swift Warnings Notes

  • warning: expression implicitly coerced from 'Int?' to Any. This warnings is triggered cause of the print function, that requires a set of Any parameters. String is a Any. In this case the compiler is telling you that it implicitly coerced the optional string into an Any object (by transforming the String value in Optional(value)).
  • note: force-unwrap the value to avoid this warning. When an object is optional and you want to access one of its methods or properties, you will need to ‘unwrap’ it.

The output of our running code will be:

Output Swift Code Compiled Date

In conclusion, you can see how easily it is to get started with the Swift programming language or at least with its syntax in Windows. Notice that as there's no official API that exposes how to do specific things in Windows, like opening a File browser, a program or related, this project is made entirely for strict learning purposes of the language in a different environment.

Happy coding !

This could interest you

Become a more social person