-
Notifications
You must be signed in to change notification settings - Fork 1
/
Optionals&Unwrap.swift
40 lines (27 loc) · 859 Bytes
/
Optionals&Unwrap.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Optionals and Unwrapping in Swift
// Optionals and Unwrapping is used to handle nil values in Swift.
// Optionals are used to represent the absence of a value.
// Optionals are used to represent the presence of a value.
// Example 01
// var name: String = "Madhusha" // non-optional variable
// name = nil // error
// we can use optionals to fix above nil error like this
var name: String? = "Madhusha" // optional variable
name = nil // no error
name = "Prasad"
print(name)
// Example 02
var x: Int? = 10
var y: Int? = 20
// var sum = x + y // error
// we can use optionals to fix above nil error like this
var sum = x! + y! // no error
print(sum)
if let value = x {
if let value2 = y {
var sum = value + value2
print(sum)
}
}
var price: Double?
print(type(of: price)) // print price variable type as Optional<Double>