標籤:ios swift
這期主要講一下關於常用控制語句以及方法的使用
首先是迴圈語句
常用的for in(這個在上期就有簡單的涉及,跟其它語言也類似)
var arrayBu = ["法師", "聖騎士", "術士", "德魯伊", "盜賊"]for item in arrayBu { println(item)}var dictionaryBu = ["職業": "法師", "模式": "競技場"]for (key, value) in dictionaryBu { println("\(key) : \(value)")}for var i = 0; i <= 5; i++ { println(i)}for i in 0...5 { println(i)}for char in "sun Wanhua" { println(char)}
然後是while與do while(跟其它語言類似,不做過多說明)
var x = 0var result = 0while x < 10 { result += x x++}println(result)var y = 0do { y--} while y > -5println(y)
接下來是條件陳述式
常用的if else (跟其它語言類似,不做過多說明)
var temp = 30if temp < 32 { println(0)} else if temp > 33 && temp < 40 { println(1)} else { println(2)}
然後是switch,這個跟其他語言有點不一樣,我們可以多練習練習
var flag = "sse"//同時滿足幾個條件,只走第一個條件//這裡不用再加break了,每次執行後預設走break//這個已經不是單一的只能放整數類型與枚舉類型了哦,我們還可以放字串與元祖等類型做依據哦switch (flag) { case "happy": println("高興") case "sad", "lose": println("悲傷") case let test where test.hasSuffix("se"): println("你猜我在幹嗎") default: println("沒什麼感覺")}var number = -5switch (number) { case 0...9: println("這是個位元") case 10...99: println("這是十位元") case 100...9999: println("這是很大的數") default: println("這是負數")}let point = (6, 6)switch (point) { case (0, 0): println("原點") case (_, 0): println("x軸") case (0, let y): println("y軸上的點\(y)") case (-2...2, -2...2): println("矩形 -2...2 地區") case let (x, y) where x == y || x == -y: println("在對角線上") case (let x, let y): println("隨意的點\(x),\(y)") default: println("某一點")}
接下來是轉移語句
常用的有continue,break,return,fallthrough(常與switch嵌套使用)
前三種跟其它語言一樣,沒有過多解釋
et str = "great minds think alick"for char in str { switch char { case "a", "e", "i", "o", "u", " ": continue default: println(char) }}for i in 1...10 { println(i) if i > 5 { break }}let tempNumber = 5var descript = "數字\(tempNumber)是"//這裡用fallthrough來起到橋樑的作用,讓這兩個語句關聯一起並執行switch tempNumber { case 2, 3, 5, 7, 11, 13: descript += "一個素數,同時也是一個" fallthrough default: descript += "整數"}println(descript)
然後就來介紹一下方法的基本介紹與使用吧
在這裡我們用func來定義方法
一般為: func 方法名(參數, 參數) ->返回參數類型 {}
調用的時候為:方法名(參數,參數)
//單參數 func sayHello(username: String) ->String { let greeting = "你好,\(username)" return greeting}println(sayHello("swh"))//多參數func sumOf(numberA:Int, numberB:Int) ->Int { return numberA + numberB}println(sumOf(10, 5))//無傳回值func sayGoodbye(username: String) { println("歡迎\(username)下次再來")}sayGoodbye("kutian")//無傳回值無參數func sayWelcome() { println("歡迎來到Swift")}sayWelcome()//單參數,傳回值為多參數,類似元祖func countString(value:String) -> (vowels:Int, consonants:Int, others:Int) { var vowels = 0, consonants = 0, others = 0 for char in value { switch String(char).lowercaseString { case "a", "e", "i", "o", "u": vowels++ case "b", "c": others++ default: consonants++ } } return (vowels, consonants, others)}let count = countString("some string in English!")println(count.vowels)//多參數,且最後一個參數給出預設值func joinString(firstValue value1:String, secondValue value2:String, betweener joiner:String = " - ") ->String { return value1 + joiner + value2}println(joinString(firstValue: "a", secondValue: "b"))//"#"為上面方式的簡寫,主要是給出參數的解釋,類似O-C,易讀func joinStringNew(#firstValue:String, #secondValue:String, betweener:String = " - ") ->String { return firstValue + betweener + secondValue}println(joinStringNew(firstValue: "w", secondValue: "a", betweener: "+"))//不定參數,可傳任意個func sumOfNumbers(numbers:Double ...) ->Double { var total:Double = 0 for num in numbers { total += num } return total}println(sumOfNumbers(1, 5, 8))//inout 表示所傳參數為其地址,所以我們再傳參數時需要加上"&"//這裡跟C語言函數類似func modifyInt(inout a:Int, inout b:Int) { a += 3 b = 6}var someInt = 3var anotherInt = 9modifyInt(&someInt, &anotherInt)println("\(someInt), \(anotherInt)")func addTwoInt(a:Int, b:Int) ->Int { return a + b}//方法在swift中也算一個類,那麼也可以定義成變數的類型var mathFunc:(Int, Int) ->Int = addTwoIntprintln(mathFunc(1, 2))//方法也可以在其它方法中充當參數或者傳回值func printMathResult(mathFunction:(Int, Int) ->Int, a:Int, b:Int) { println(mathFunction(a, b))}println(printMathResult(addTwoInt, 3, 5))func firstFunction(i:Int) ->Int { return i + 1}func secondFunction(i:Int) ->Int { return i + 2}func chooseFunction(which:Bool) -> (Int) ->Int { return which ? firstFunction : secondFunction}//這裡targetFunction就等價於first與second某一個方法()let targetFunction = chooseFunction(false)println(targetFunction(1))//嵌套使用//在方法中,我們還可以定義方法,並且調用這些定義的func newChooseFunction(which:Bool) -> (Int) -> Int { func firstFunctionNew(i:Int) ->Int { return i + 1 } func secondFunctionNew(i:Int) ->Int { return i + 2 } return which ? firstFunction : secondFunction}let targetFunctionNew = newChooseFunction(false)println(targetFunctionNew(1))
好啦,就介紹這麼多吧
本文出自 “東軟iOS校友群的技術部落格” 部落格,請務必保留此出處http://neusoftios.blog.51cto.com/9977509/1669808
swift篇第二期:控制語句與方法的使用