//
// [Link]
// VipinSwift
//
// Created by MASH Virtual on 28/11/20.
// Copyright © 2020 Vipin. All rights reserved.
//
import Foundation
class ChapterSwitch{
func callPrintFunctions(){
compoundStatement()
}
func atLeastOneExecutableStatement(){
//The body of each case must contain at least one executable statement. It
is not valid to write the following code, because the first case is empty:
let anotherCharacter : Character = "s"
switch anotherCharacter {
case "a":
print("a this is")
case "b":
print("b this is")
default:
print("default value")
}
func compoundStatement(){
//combine the two values into a compound case, separating the values with
commas.
let anotherCharacter: Character = "a"
switch anotherCharacter {
case "a", "b":
print("value exists")
default:
print("default")
}
}
func interValMatching(){
let approximateCount = 62
let countedThings = "moon orbiting Saturn"
let naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings)")
func tuplesAndSwitch(){
let somePoint = (1,1)
switch somePoint {
case (0,0):
print("\(somePoint) is at the origin")
case (_,0):
print("\(somePoint) is on the x-axis")
case (0,_):
print("\(somePoint) is on the y-axis")
case (-2...2,-2...2):
print("\(somePoint) inside the box")
default:
print("default item")
}
}
func ValueBindings(){
//temporary constants or variables
//This behavior is known as value binding, because the values are bound to
temporary constants or variables within the case’s body.
let anotherPoint = (2, 0)
switch anotherPoint {
case (let x , 0):
print("on the x-axis with the x value of \(x)")
case (0, let y):
print("on the y-axis with the y value of \(y)")
case let(x, y):
print("somewhere else at (\(x) , \(y))")
}
}
func switchWhere(){
//A switch case can use a where clause to check for additional conditions.
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint{
case let (x, y) where x == y:
print("\(x) and \(y) is on the line x == y")
case let (x, y) where x == -y:
print("\(x) and \(y) is on the line x == -y")
case let (x, y):
print("\(x) and \(y) it just arbitrary point")
}
}
func multipleSwitchCase(){
//Multiple switch cases
let someCharacter: Character = "e"
switch someCharacter{
case "a", "e", "i", "o", "u":
print("vowels are available")
case "b","c","d","f","g","h","j","k","l","m","n",
"p","q","r","s","t","v","w","x","y","z":
print("\(someCharacter) is a consonant")
default:
print("some case are not vowel or not consonant")
func fallthroughExample(){
//In Swift, switch statements don’t fall through the bottom of each case
and into the next one. That is, the entire switch statement completes its execution
as soon as the first matching case is completed.
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe{
case 2, 3, 5, 7, 11, 13, 17, 19:
description += "a prime number and also"
fallthrough
default:
description += " an integer"
}
print(description)
func guardStatementExample(person:[String : String]){
guard let name = person["name"]else{ return }
print("Hello \(name)!")
guard let location = person["location"]else{
print("I hope the wheater is nice near to you.")
return
}
print(" i hope the wheather is nice in\(location)")
}
func differenceWithGuard(person:[String : String]){
if let name = person["name"]{
print("name have local scope")
}else{
print("else part")
}
}
func switchAndEnum(){
enum CarModel {
case Standard, Fast, VeryFast
}
let car = [Link]
switch car {
case .Standard: print("Standard")
case .Fast: print("Fast")
case .VeryFast: print("VeryFast")
}
}
func switchAndOptional(){
let optionalStr : String? = nil
switch optionalStr{
case nil:
print("value is nil")
case let abc as String?:
print("not optional")
case let xyz as String:
print("this is optional")
default:
print("this is default")
func practiceMethod(){
typealias mdyTuple = (month: Int, day: Int, year: Int)
let fredsBirthday = (month: 4, day: 3, year: 1973)
let bobsBirthday = (month: 4, day: 3, year: 1973)
let susansBirthday = (month: 4, day: 3, year: 1973)
var message : String = String()
var theMDY : mdyTuple = (month: 4, day: 3, year: 1973)
switch theMDY
{
//You can match on a literal tuple:
case ([Link], [Link],[Link] ):
message = "the day Fred was born"
case (3, 15, _):
message = "Beware the Ides of March"
case ([Link], [Link], let year) where year >
[Link]:
message = "bobs birthday"
case ([Link], [Link], let year)
where year > [Link]:
message = "susan birthday"
default:
print("default")
}
}