Created
January 27, 2020 15:26
-
-
Save alokc83/f4656bf9a40d38e7af31a602eb4aefe0 to your computer and use it in GitHub Desktop.
For Loops 2
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
// ///////////////////////////////////////////////////////// | |
// For Loops 2 | |
// ///////////////////////////////////////////////////////// | |
import Foundation | |
/// Use of .some in For loop with `for case let` | |
func usingForCaseLet() { | |
print("\n-----------Result for function \(#function) -----------") | |
// .some | |
let data: [Any?] = ["IronMan", nil, 1989, "SpiderMan#1"] | |
for case let .some(element) in data { | |
print(element) | |
} | |
// .optional | |
for case let element? in data { | |
print("With ? \(element)") | |
} | |
} | |
usingForCaseLet() | |
/// Using `case let` to filter the array | |
func usingForCaseLetForFiltering() { | |
print("\n-----------Result for function \(#function) -----------") | |
enum CarState { | |
case running(speed: Int) | |
case stopped | |
case idling | |
} | |
let runningSpeeds: [CarState] = [ | |
.running(speed: 40), .stopped, | |
.idling, .running(speed: 90), .idling, | |
.running(speed: 10) | |
] | |
for case let .running(speed) in runningSpeeds { | |
print("speed is \(speed)") | |
} | |
} | |
usingForCaseLetForFiltering() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment