Skip to content

Instantly share code, notes, and snippets.

@alokc83
Created January 27, 2020 15:26
Show Gist options
  • Select an option

  • Save alokc83/f4656bf9a40d38e7af31a602eb4aefe0 to your computer and use it in GitHub Desktop.

Select an option

Save alokc83/f4656bf9a40d38e7af31a602eb4aefe0 to your computer and use it in GitHub Desktop.

Revisions

  1. alokc83 created this gist Jan 27, 2020.
    47 changes: 47 additions & 0 deletions acdotcom-st-for-loops2.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@

    // /////////////////////////////////////////////////////////
    // 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()