Last active
May 22, 2018 17:54
-
-
Save dineshba/e571022afc5ad7b62e02a4dce01f0270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Programmer { | |
let brain: Brain | |
let laptop: Laptop? | |
let name: String | |
} | |
struct Brain { | |
} | |
struct Laptop { | |
let name: String | |
let fan: Fan? | |
} | |
struct Fan { | |
let name: String | |
} | |
let programmer = Programmer(brain: Brain(), laptop: nil, name: "Ramu") | |
let laptop = Laptop(name: "mac", fan: Fan(name: "mac fan")) | |
let programmerWithLaptop = Programmer(brain: Brain(), laptop: laptop, name: "Somu") | |
// Let's say we want to get the name of the programmer's laptop | |
// and also the name of the fan in laptop | |
// Note: Fan is also optional for a laptop | |
// How will we do it using if-let ? | |
var laptopName: String? // by default, laptopName(optional) will take `nil` value. | |
var laptopFanName: String? | |
if let laptop = programmer.laptop { | |
laptopName = laptop.name | |
if let fan = laptop.fan { | |
laptopFanName = fan.name | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment