Swift学习笔记

let 定义常量

var定义变量

countElements()字符串长度

isEmpty  判断是否空字符串

append()/ +=    数组追加元素

insert()数组插入元素

removeAtIndex() 数组删除元素

count数组长度

元组的定义:

let status = {success:200, fail:500, description:"显示信息"}

数组的遍历:

var peopleList: String[] = ["小明","小李"]

for item in peopleList {

println(item)

}

for (index,value) in enumerate(peopleList){

println("id: \(index + 1) name: \(value)")

}

字典的定义:

var airports : Dictionary<String,String> = ["CCA":"China Air","KSE":"Korean Air"]

removeValueForKey()字典删除元素

字典的遍历:

for (airportCode,airportName) in airports {

}

for airportCode in airports.keys {

}

for airportName in airports.values {

}

获得字典的键或值的集合

let airportCodes = Array(airports.keys)

let airportNames = Array(airports.values)

switch 语句  不需要break 最后都不符合条件可执行default 

switch findd{

case "a","b","c": //字符串情况可以多项

println("not find")

fallthrough   //加上这句可以贯穿下去 同时也走下个分支

case "d":

println("ok")

case 0…3:  //数字情况可以判断中间值

default:

}

函数的定义

func find(id:String,name:String) -> String{

return "ok"

}

func find(id:String,name:String) -> (selectId: int,selectName:String){ //也可使用元组返回值

return (1,"小明")

}

fund find(type:Bool) -> (Int) -> Int { //传入int型  返回也是int型

}

let player = find();

println("\(player.selectId) , \(player.selectName)")

相关推荐