A place to breathe

Monday, July 7, 2014

Swift Syntax overview - more switch

Swift has powerful switch statement construct. Switch statement can compare multiple values, range, as well as tuples. The case statement can also do logic comparison. The following code demonstrates its capability

let myCar = "Ford Focus"
let yourCar = "Ford Fiesta"



switch car {

  case “BMW” :

      let myComment = “Need more cash”


   case let x where x.hasSuffix(“Ford”)   // put the content into   temp variable "x" and compare 


      let myComment = “your car now is \(x) ? “    // interpret the var x inside the bracket with slashes


  default:

     myComment = “Any car will do for me as long as it is cheap”

}
 


No comments:

About Me

I'm currently a software engineer. My specific interest is games and networking. I'm running software company called Nusantara Software.