标签: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 = 0
var result = 0
while x < 10 {
result += x
x++
}
println(result)
var y = 0
do {
y--
} while y > -5
println(y)接下来是条件语句
常用的if else (跟其它语言类似,不做过多说明)
var temp = 30
if 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 = -5
switch (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 = 5
var 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 = 3
var anotherInt = 9
modifyInt(&someInt, &anotherInt)
println("\(someInt), \(anotherInt)")
func addTwoInt(a:Int, b:Int) ->Int {
return a + b
}
//方法在swift中也算一个类,那么也可以定义成变量的类型
var mathFunc:(Int, Int) ->Int = addTwoInt
println(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
标签:ios swift
原文地址:http://neusoftios.blog.51cto.com/9977509/1669808