dcsimg
 

An Intro to Swift Programming - on Windows Even

by Brad Jones
An Intro to Swift Programming - on Windows Even

The Swift language lives up to its name, and offers a number of modern features too, like automated memory management. It even works on Windows.

At a little over six years old, the Swift programming language is considered relatively new to the programming scene. Swift is a compiled, general-purpose programming language that was developed by Apple and the open source community. While Swift is not at the top of the charts, it has been on the Top 20 list from TIOBE for many years, outperforming other languages such as Perl, Lisp, and Ruby.

When launched, Swift was seen primarily as a development language for Apple developers. In its short life, however, it has evolved beyond targeting the Apple platforms (such as iOS, macOS, tvOS, and watchOS) to also target other platforms, including Linux. Possibly to the surprise of some, it is also possible to write and run Swift code on Windows 10 systems.

Swift can be described in much the same way as many of today's newer programming languages. Terms such as modern, clean, fast, and safe are all used to describe Swift. Better reasons to look at the language, however, are things such as automatic memory management, inferred types, and built-in error handling. Swift was built as a safer language to use than Objective-C; It was built with a design goal of being a safer C-based language.

Hello World in Swift

The most common starting point when talking about a programming language is to show the code for a "Hello World" application. By seeing the full code for the Hello World application, programs can quickly get a sense of a programming language. In Swift, the code for this is simply:

Print(“Hello World!”)

This single line of code will display Hello World! If you've coded in other C-based languages, you might question the single line of code, you might notice the lack of a semicolon, the lack of a method or function to start off the program (a main() method), and even the lack of including any libraries. The intent of Swift was to be simpler than your average C-based language.

Trying Swift Yourself

If you want to try out the "Hello World!" code yourself, there are a variety of options including installing an IDE or using an interactive site repl.it. You can go to repl.it and install the Swift language repl. Once added, you can enter the line of code and run it as shown here:

A variety of IDEs also support Swift, including Atom, AppCode, CLionm CodeRunner, SublimeText, Visual Studio Code (VS Code), and XCode. You can also download Swift for several platforms from https://swift.org/download/.

A Quick Look at Some Swift Code

The best way to get a quick perspective on a programming language is to see a little code. The following is a code listing that is a bit silly but illustrates creating and setting a variable. It then uses the variable called month in a program flow statement. In this case a switch statement is being used. This is a complete Swift listing:

let month = "December"
switch month {
case "January", "February", "March":
    print("Brrr. It's cold outside!")
case "April", "May":
    print("Spring is in the air!")
case "June", "July", "August":
    print("It's getting hot! ")
case "September", "October":
    print("Fall is in the air!")
case "November":
    print("Turkey month in the USA!")
case "December":
    print("Final month of the year!")
default:
    print("What calendar are you using!")
}

When you run this program, you will see the following output:

Final month of the year!

You can change the month "December" in the first line to one of the other months and see how the output changes.

Looping in swift can be done with a few statements. The for statement is familiar to C-based programmers. The use of for in Swift is slightly different. The following program displays the odd numbers between zero and one hundred using a simple for statement. Again, this code will run as presented in Swift:

var cap = 100
for ctr in 0.. 0 {
     print(i)
  }
}

As you can see in this code, a simple variable called cap is created using var. A for statement is then used to look using a value of x that starts at a value of 0 and increments by one as long as it is less than the value of cap. For each loop in the for statement, the modulus operator is used to determine if the value of x is odd or even. This is accomplished by seeing if there is a remainder when dividing x by 2. If so, then the value of the counter (x) is printed. The result is the odd numbers starting with 1 are displayed all the way up to 99.

Swift can also be used to create classes and objects. A class is created using a class keyword. The following simplistic code sample creates a basic Person class with a first name, last name and age for each person. It also contains a constructor to initialize a class as well as a function to get a displayable string based on the information in the class. This should look relatively straightforward:

class Person {
  var firstName: String
  var lastName: String
  var age: Int
 
  init(First: String, Last: String, Age: Int) {
    self.firstName = First
    self.lastName = Last
    self.age = Age
  }
 
  func description() -> String {
    return "\(firstName) \(lastName) is \(age)."
  }
}
 
let me = Person(First: "Brad", Last: "Jones", Age: 21)
print("Last name is: \(me.lastName)")
print(me.description())

Also included are a couple of lines to create an object called me that uses the Person class. The value of the last name is then printed followed by printing the string created by the description function. When run, the output from this listing is:Last name is: Jones Brad Jones is 21.

Parting Thoughts

This is a swift look at Swift that barely scratches the surface of what it can do. As you can see from the code listings provided, Swift does work to be simpler than C-based languages, while also working to maintain features such as object orientation. With its migration to Linux and the Windows platform, Swift has expanded from being a language for only Apple developers. In the world of programming languages, it is still young. It will be interesting to see where it stands in a few more years.

This article was originally published on Wednesday Feb 17th 2021
Home
Mobile Site | Full Site