Optional binding and Optional Chaining

Swift has a feature that lets users to assign optional value to a variable or a constant. Optional variable or constant can contain a value or a nil value. Let us take the following example which tries to find a given string in a array of string.

Optional Binding

var fruits = ["Apple","Orange","Grape","Mango"]
let searchIndex = find(fruits, "Apple”)

The searchIndex would return value if the fruit exists or nil value if it doesn’t exist.

println("Fruit index is \(searchIndex)”)


The proper way to handle this by using Optional binding method.

if let searchIndex = searchIndex {
	println("Fruit index is \(searchIndex)")
} else {
	println("Not available")

This would ensure only when searchIndex has a value the println with searchIndex gets executed.

Optional Chaining

Optional chaining is the way by which we try to retrieve a values from a chain of optional values. Let us take the following example classes.

class School {
	var director:Person?

class Person {
	var name: String = ""
	init(name: String) {
		self.name = name


var school = School()
var person = Person(name: "Jason")
school.director = person

The director property in School class is optional, when you try to access subsequent values from director property becomes optional (? mark after director when accessing name property). You can handle these optionals as shown below.

if let name = school.director?.name {
	println("Director name is \(name)")
} else {
	println("Director yet to be assigned")
In Category: Apple, ios

Ravi Shankar

A polyglot software developer and now exploring Swift and iOS development. If you would like to learn from me then check out services page.

Show 0 Comments
No comments yet. Be the first.

Leave a Comment