• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips
You are here: Home / Apple / Optional binding and Optional Chaining

Optional binding and Optional Chaining

May 7, 2015 By Ravi Shankar 6 Comments

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

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

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

[code language=”swift”]println("Fruit index is \(searchIndex)”)
[/code]

 

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

[code language=”swift”]if let searchIndex = searchIndex {
println("Fruit index is \(searchIndex)")
} else {
println("Not available")
}[/code]

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.

[code language=”swift”]class School {
var director:Person?
}

class Person {
var name: String = ""
init(name: String) {
self.name = name
}
}
[/code]

 

[code language=”swift”]
var school = School()
var person = Person(name: "Jason")
school.director = person
school.director?.name
[/code]

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.

[code language=”swift”]
if let name = school.director?.name {
println("Director name is \(name)")
} else {
println("Director yet to be assigned")
}[/code]

Filed Under: Apple, ios Tagged With: Apple, Optional bindings, Optional chaining, Swift

Reader Interactions

Comments

  1. james says

    January 3, 2018 at 11:29 am

    Nice work … Thankyou

    Reply
  2. Anilkumar says

    January 25, 2018 at 4:01 pm

    Nice explanation.

    Reply
  3. sravani says

    August 29, 2018 at 3:59 pm

    nice explanation thnq

    Reply
  4. Ganesh says

    November 4, 2018 at 4:19 pm

    Simple and straight explanation. Good!

    Reply
  5. Dharanikumar says

    December 17, 2018 at 6:50 am

    Nice Explanation

    Reply
  6. Ravi Prajapat says

    May 18, 2019 at 6:03 am

    Brilliant explanation.. Thanks!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy