Info

Programmer by day, artist by night

Some interesting Swift tidbits with practical code examples. Implications: use them in your projects for more readable and maintainable code.

Declaring multiple values in a single line
Multiple variables or constants can be declared in a single line using commas to separate them:

var red = 141.0, green = 185.0, blue = 230.0, alpha = 1.0

 
They can also be declared in a single line by adding the type annotation to the final variable:

var red, green, blue, alpha: Double

 
Large number readability
For large numbers, we can use underscore for better readability:

let mapRadius = 100_000

 
Using typealias for readable code
Using typealias for more readable code. For example, var program contains AnyObject, the primary purpose of creating var program is to use it as a property list. So instead of declaring:

var program: AnyObject?

 
We can typealias PropertyList = AnyObject, and use PropertyList for the var program type.

typealias PropertyList = AnyObject
var program: PropertyList?

 
Defining optional variables with nil value
When defining an unknown optional variable it doesn’t need to be explicitly set to nil. Swift automatically sets the variable to nil. For example:

var error: String? = nil // Not needed
var error: String?       // Swift automatically sets it to nil

 
Multiple optional bindings for concise and legible code
Multiple optional bindings help avoid many layers of hard to read nested code. For example,

The pyramid of nested optional bindings (hard to read):

if let firstNumber = Int("123") {
    if let secondNumber = Int("456") {
        if firstNumber < secondNumber {
            print("\(firstNumber) < \(secondNumber)")
        }
    }
}
// prints "123 < 456"

 
Multiple optional bindings (elegant):

if let firstNumber = Int("123"), secondNumber = Int("456") where firstNumber < secondNumber {
    print("\(firstNumber) < \(secondNumber)")
}
// prints "123 < 456"

 
Nil coalescing operator
Helps provide a default value if the evaluation is nil. For example,

tipPercentageForRestaurant is nil, so tipPercentage is 0.0:

var tipPercentageForRestaurant: Double?
let tipPercentage = tipPercentageForRestaurant ?? 0
print(tipPercentage)
// prints 0.0

tipPercentageForRestaurant is 0.15, so tipPercentage is 0.15

var tipPercentageForRestaurant: Double?
tipPercentageForRestaurant = 0.15
let tipPercentage = tipPercentageForRestaurant ?? 0
print(tipPercentage)
// prints 0.15

 

Comments

2 Comments

Post a comment
  1. November 24, 2015

    Nice, I’ll use some of this in my iOS project. Multiple optional bindings are definitely elegant!

    • November 24, 2015

      Thanks Jefferson. Will try to keep the post updated as I come across other interesting snippets.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS