天天看點

swift _ 控制流

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

//For循環

//For in

for index in 1...5{

    print(index)

}

//如果你不需要知道區間内每一項的值,你可以使用下劃線(_)替代變量名來忽略對值的通路

let power = 6

for _ in 1...power {

    print("++++++")

}

//元組周遊

let numberOfLegs = ["spider": 8, "ant": 6]

for (code,cont) in numberOfLegs{

    print(code)

    print(cont)

}

//let http = (4,"rrr")

//http.0

let count = 3_000_000_000_000

let countedThings = "stars in the Milky Way"

var naturalCount: String

switch count {

case 0:

    naturalCount = "no"

case 1...3:

    naturalCount = "a few"

case 4...9:

    naturalCount = "several"

case 10...99:

    naturalCount = "tens of"

case 100...999:

    naturalCount = "hundreds of"

case 1000...999_999:

    naturalCount = "thousands of"

default:

    naturalCount = "millions and millions of"

}

print("There are \(naturalCount) \(countedThings).")

let somePoint = (1, 1)

switch somePoint {

case (0, 0):

    print("(0, 0) is at the origin")

case (_, 0):

    print("(\(somePoint.0), 0) is on the x-axis")

case (0, _):

    print("(0, \(somePoint.1)) is on the y-axis")

case (-2...2, -2...2):

    print("(\(somePoint.0), \(somePoint.1)) is inside the box")

default:

    print("(\(somePoint.0), \(somePoint.1)) is outside of the box")

}

let anotherPoint = (2, 0)

switch anotherPoint {

case (let x, 0):

    print("on the x-axis with an x value of \(x)")

case (0, let y):

    print("on the y-axis with a y value of \(y)")

case let (x, y):

    print("somewhere else at (\(x), \(y))")

}

let yetAnotherPoint = (1, -1)

switch yetAnotherPoint {

case let (x, y) where x == y:

    print("(\(x), \(y)) is on the line x == y")

case let (x, y) where x == -y:

    print("(\(x), \(y)) is on the line x == -y")

case let (x, y):

    print("(\(x), \(y)) is just some arbitrary point")

}

let puzzleInput = "great minds think alike"

var puzzleOutput = ""

for character in puzzleInput.characters {

    switch character {

    case "a", "e", "i", "o", "u", " ":

        print("1")

       continue

    default:

        puzzleOutput.append(character)

    }

}

let numberSymbol: Character = "三"  // 簡體中文裡的數字 3

var possibleIntegerValue: Int?

switch numberSymbol {

case "1", "١", "一", "๑":

    possibleIntegerValue = 1

case "2", "٢", "二", "๒":

    possibleIntegerValue = 2

case "3", "٣", "三", "๓":

    possibleIntegerValue = 3

case "4", "٤", "四", "๔":

    possibleIntegerValue = 4

default:

    break

}

if let integerValue = possibleIntegerValue {

    print("The integer value of \(numberSymbol) is \(integerValue).")

} else {

    print("An integer value could not be found for \(numberSymbol).")

}

//fallthrough

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

case 1:

    description += "34"

default:

    description += " an integer."

}

print(description)

let finalSquare = 25

var board = [Int](count: finalSquare + 1, repeatedValue: 0)

board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02

board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08

var square = 0

var diceRoll = 0

gameLoop: while square != finalSquare {

    if ++diceRoll == 7 { diceRoll = 1 }

    switch square + diceRoll {

    case finalSquare:

        // 到達最後一個方塊,遊戲結束

        break gameLoop

    case let newSquare where newSquare > finalSquare:

        // 超出最後一個方塊,再擲一次骰子

        continue gameLoop

    default:

        // 本次移動有效

        square += diceRoll

        square += board[square]

    }

}

print("Game over!")