A simple introduction to Swift

At the WWDC 2014 Apple introduced a brand new programming language called Swift,. It’s a language that has been created by Apple from the ground up to be both very efficient and very beautiful.

Swift uses the same API’s as C but in my opinion is easier to learn and get a grasp if you come from a web development background like me.

If you are just as exited about this language as I am, then this is the article for you.

Variables and constants

Declaring variables in Swift, if you come from a Javascript background, should be very familiar; you declare a variable by using the var keyword:

var myNumber = 40

You can, of course, also set constants; to define a constant you need to use the let keyword:

let name = "Developer Drive"

If you have used Objective-C, or in this case even Javascript, one thing you will notice from the sample code I just wrote is that I didn’t add any semi colons. That is because Swift doesn’t require them (although you can include them if you prefer).

Manipulating strings

This is another part of the language where you should feel at home if you work with Javascript. Concatenating strings is done as you would see in any of your Javascript files, you just need to use the “+” sign:

var firstName = "Developer"
var lastName = " Drive"
var name = firstName + lastName

By the way, if you want to print this to the screen all you need to do is call println:

println(name)

Inserting variables into a string is also really simple; you need to use “\” and then place the variable inside some parenthesis:

var number = 20
var timeLeft = "You have \(number) minutes left."

As you can see, everything is written in a simple and structured manner.

Arrays

One thing every language needs to handle is arrays. We always need them, and it’s a huge help if they are simple to set up.

They are simple in Swift, and again, will be familiar to anyone who has used Javascript:

var names = ["Leslie","April","Tom","Andy","Ann","Ben"]

As you can see this is a really simple and clean method.
If you want to count the elements use the count property:

var numberNames = names.count

Adding elements to your array is also fairly straight forward:

// Adding a single element
names += "Jerry"

// Adding several elements
names += ["Donna", "Chris"]

Dictionaries

The other collection type we have in Swift is dictionaries. In this type of collection every value we have in the array is associated with a unique key, like so:

var socialNetworks = ["FB" : "Facebook", "TW" : "Twitter", "G+" : "Google+"]

And as you would expect, you can iterate through them using a for loop:

for (abbr, name) in socialNetworks {
    println("\(abbr) = \(name)")
}

You can also call out a value very easily by specifying the key for it, and if you want to change that value just assign it with a “=” sign:

println(socialNetworks["FB"])
socialNetworks["FB"] = "Vine"

Functions

The next step is to see how we can create simple function in swift and we do that by using the func keyword:

func helloRuby() -> String {
    return "Hello Ruby"
}
helloRuby()

As you can see, the syntax for defining a function is really simple and great to look at, even when it comes to calling the function all you need to do is write the name of the function and if you don’t have any parameters, just open and close parenthesis.

Of course, we can also have parameters if we need them. We just declare them inside the parenthesis, after the name of the function:

func hello(name: String) -> String {
    return "Hello \(name)."
}
hello("Ruby", "Tuesday")

Classes

Classes, unlike in Objective-C, are all created in the same .swift file, and there is no need to separate implementation and external interface.

A class is created by using the keyword “class” followed by the name of the class:

class Show {
    var numberOfEpisodes = 20
    func number() -> String {
        return "The Show has \(numberOfSides) episodes."
    }
}

You can create a new instance of the class by simply adding an opening and closing parenthesis after the name:

var show = Show()

You can also change the properties really easily by using dot notation:

show.numberOfEpisodes = 35

Statements and Loops

We’ve already taken a look at how to use the for loop with an array but there are more statements and loops I want to show you.

The first statement we usually learn in any language is the if statement, and creating these in Swift is as simple as you would hope:

var name1 = "Tom"
var name2 = "Tom"
if name1 == name2 {
    println("Identical")
}

As you can see parenthesis in Swift aren’t necessary for the if statement to work.

Switch statements are another thing we end up using quite a lot when coding, and are written like:

switch babyNames {
 case "Ben":
 println("Maybe")
 case "Jerry":
 println("No Really")
 default:
 println("What about we go with Tom ?")
 }

Conclusion

As you can see from these basics Swift is a very well written language that does a great job and should be highly appealing to any web developer hoping to move into mobile. It’s simple, but it allows you to everything Objective-C allows.

Sara Vieira is a freelance Web Designer and Developer with a passion for HTML5/CSS3 and jQuery. You can follow her on twitter or check out her website. More articles by Sara Vieira
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress