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:

varmyNumber = 40

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

letname = "DeveloperDrive"

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:

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:

varnumber=20vartimeLeft="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:

varnumberNames = names.count

Adding elements to your array is also fairly straight forward:

// Adding a single elementnames+="Jerry"// Adding several elementsnames+=["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:

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

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

for(abbr,name)insocialNetworks{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:

funchelloRuby()->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:

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.