Skip to content

Instantly share code, notes, and snippets.

@dineshba
Last active May 30, 2018 12:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dineshba/9ceb91ccdd482912d680452f5add82ae to your computer and use it in GitHub Desktop.
var optionalValue: Int?
if let value = optionalValue { // unwraps optionalValue and assigns
// it to variable named value
print(value) // type of value = Int
else {
print("not able to unwrap optionalValue as it is nil")
}
//Let's rewrite the sum function using if-let
func sum(_ optionalAugend: Int?, _ optionalAddend: Int?) -> Int? {
if let augend = optionalAugend,
let addend = optionalAddend {// `,` is AND operation.
// If both variables are unwrapped, enter into if-case
return augend + addend
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment