Swift ♥

I’m sorry to disappoint all the non-techies out there but this is not a post about Taylor Swift. It’s about a few things that I really love about Swift, the programming language introduced by Apple.

#1 Playgrounds

noun: a place where people can play

When I want to learn a new language or a new language feature, I always create a new project. The Problem with this approach is that you need to compile and run your project every time you make a change.

Playgrounds makes it much easier to learn Swift because it executes your code as soon as you make a change. Furthermore it provides you with a quick look window that display graphics, lists of results, or graphs of a value over time. I found myself learning the language much faster compared to my previous approach.

#2 No Semicolons

In languages such as Objective-C and Java a semicolon announces an end of a statement and as programmers it’s in our blood to put a ; at the end of a line. However in Swift semicolons are truly optional Yay!

This really improves the readability of your code but if you choose to write multiple statements in one line in Swift you need to use a semicolon anyway. Eh! multiple lines in one statement is a bad practice anyway.

#3 Strings

#import <stdio.h>

Objective-C did introduce neat tricks for C Strings and Swift has made things even better.

  • Concatenation
// Swift
var hello = "Hello"
var world = "World"
println(hello + " " + world)
// Objective-C
NSString *hello = @"Hello";
NSString *world = @"World";
NSString *helloWorld = [NSString stringWithFormat:@"%@ %@", hello, world];
NSLog(@"%@", helloWorld);

As you can see from the above example, Swift has made String concatenation much simpler. You can just use + like in Java to aggregate two Strings together, beautiful!.

  • Interpolation

Swift has a real cool way of doing string interpolation too.

// Swift
var vision = "My software never has bugs. "
var reality = "It just develops random features."
println("\(vision) \(reality)")
// Objective-C
NSString *vision = @"My software never has bugs. ";
NSString *reality = @"It just develops random features.";
NSLog(@"%@ %@", vision, reality);

#4 Inferred types

Wouldn't it be great if you can omit the type declaration in your code ? well with Swift you can do that. Swift can determine the type of a variable at compile time without the type declaration.

var myStr: String = "Swift is Smart"
// Yeah I know its a String, because I'm smart like that
var newStr = "Swift is Great"

I think this is a great safety feature that will prevent crashes at run time.

#5 Closures

Well that was sort of my reaction when I got the hang of using blocks in Objective-C. Blocks made complicated async callbacks much easier to tackle. Closures are Swift’s answer to blocks.

let movies = ["Pulp Fiction", 
"God Father", "Lord of the Rings", "Avengers"]
// The long way
func isBefore(one:String, two:String)->Bool{ return one > two}
let sortedStrings = movies.sorted(isBefore)
// Creating a separate method to do the sorting is boilerplate
let cleanerSorted = movies.sorted({
(one:String, two:String) -> Bool in
return one > two
// Cleaning up more (Parameter type can be removed)
movies.sorted({(one, two) -> Bool in return one > two })
// Cleaning up some more (removing return type and return)
animals.sorted({(one, two) in one > two })
// Even more Whoa
animals.sorted(){$0 > $1}

I’m not a Swift expert by any means and there are so many new concepts to learn. I would be really happy to hear about your experience with Swift and any tutorials or methodologies that you used to get up to speed with Swift.

For anyone starting with Swift, I would really recommend you to check out Swift by Tutorials which is an amazing resource to learn Swift swiftly.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.