Skip to content

Instantly share code, notes, and snippets.

@alokc83
Created January 27, 2020 15:26
Show Gist options
  • Save alokc83/f4656bf9a40d38e7af31a602eb4aefe0 to your computer and use it in GitHub Desktop.
Save alokc83/f4656bf9a40d38e7af31a602eb4aefe0 to your computer and use it in GitHub Desktop.
For Loops 2
// /////////////////////////////////////////////////////////
// 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