Skip to content

Commit b71c5eb

Browse files
Update optuples.md
1 parent d61ac0d commit b71c5eb

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

optuples.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,24 @@ nil
259259
```
260260

261261
#### Using Optional Chaining
262-
This method will be discussed after seeing about Classes in Swift.
262+
Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil. If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil, the property, method, or subscript call returns nil. Multiple queries can be chained together, and the entire chain fails gracefully if any link in the chain is nil. The `?` symbol is suffixed to the `Optional` variable to perform Optional Chaining. The benefit of this over Forced Unwrapping is that it doesn't throw error if `nil` is found. The result is always given as an `Optional`. Instead, it returns `nil` value. The following example shows Optional chaining using nested Tuples as we haven't seen about classes and structs in Swift.
263+
264+
**Example :**
265+
```swift
266+
var a: Int? = 1, b: Int? = 8, c: Int? = 5, d: String? = "Hello"
267+
var innerTup1: (a: Int?, b: Int?, c: Int?)? = (a, b, c)
268+
var innerTup2: (c: Int?, d: String?)? = (c, d)
269+
var outerTup: (one: (a: Int?, b: Int?, c: Int?)?, two: (c: Int?, d: String?)?)? = (innerTup1, innerTup2)
270+
print(outerTup?.one?.b?.isMultiple(of: 4))
271+
outerTup?.two = nil
272+
print(outerTup?.two?.d)
273+
```
274+
275+
**Output :**
276+
```
277+
Optional(true)
278+
nil //since two is set as nil
279+
```
263280

264281
Now that we have seen about Optionals and Tuples in Swift, we can see about Variables, Literals and Constants in Swift.
265282

0 commit comments

Comments
 (0)