Playground-noun:a place where people can playimport uikit//------------------------------------------------------- -----------------------//1. Basic use//switch and OC difference://1> do not need to write break//2> each branch of the conditions of the instructions can not write//3> case If you take multiple values, you may use "," separated var grand = "a" var Result:strin Gswitch grand.uppercasestring {case "A": result = "excellent \ (grand)" Case "B": result = "good" case "C": result = "Medium" case "D", "E", "F": result = "difference" Default:result = "Unknown"}//--------------------------------- ---------------------------------------------//2. Variable/Chang value//In case matching, you can bind the value in switch to a specific constant or variable to use var point = (Ten) in the case's statement (let X, 0): Result = "This point is on the x axis, the x value is \ (x)" case (0, let y): result = "This point is on the Y axis, the Y value is \ (y)" Case let (x, y): result = "The x value of this point is \ (x), Y value is \ (y)"} ------------------------------------------------------------------------------//3. where//use where can increase the judging condition var point1 = ( -10) switch Point1 {case let (x, y) where x = = Y:result = "On the \ \ Diagonal "Case let (x, y) where x = =-y:result =" On/diagonal "Default:result =" not on diagonal "}//------------------------- -----------------------------------------------------//4. fallthrough//after executing the current case, continue executing the following case or the default statement var num = 20var str = "\ (num) is" switch num {case 0...50:STR + = "0~50 The "Fallthroughdefault:str + =" Integer "}
Swift Basic usage-switch usage