Created
January 20, 2020 14:35
-
-
Save alokc83/8a0f9d0f836780a6e5355e43362bbc3c to your computer and use it in GitHub Desktop.
Show various way to use for loops
This file contains hidden or 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
import Foundation | |
let ironman = (name: "Tony", password: "M#n") | |
let spiderman = (name: "Peter", password: "Spid3r") | |
let deadpool = (name: "Wade", password: "w1lls0n") | |
let users = [ironman, spiderman, deadpool] | |
func firstLoop() { | |
//simply printing name | |
for user in users { | |
print(user.name) | |
} | |
} | |
// Just like switch, we can user case with a tuple to match specific values inside the tuples. | |
func caseInLoop() { | |
for case("Wade", "w1lls0n") in users { | |
print("user Wade’s pass is w1lls0n") | |
} | |
} | |
// You can also bind local constants to values of each tuple: | |
func bindingLocalConstants() { | |
for case (let name, let pass) in users { | |
print("User \(name) ’s pass is \(pass)") | |
} | |
} | |
//You can also rearrange it in tuple format as below: | |
func rearrangingInTuple() { | |
for case let (name, pass) in users { | |
print("User \(name) ’s pass is \(pass)") | |
} | |
} | |
// You can also mix and match above mentioned techniques | |
func mixAndMatch() { | |
for case let (name, "Spid3r") in users { | |
print("User \(name) ’s pass is Spid3r") | |
} | |
} | |
// All of the above technique filters out the array for pass phrase. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment