元组内的值可以是任何类型的值,并不要求相同的类型
var httpError = (404,"Not found")
let xiaoming = ("boy",1.75,"student",20)
//可以将一个元组内容分解为单独的变量和常量
let (statusCode,statusMessage) = httpError
println("这是元组中的第一个元素\(statusCode)")
println("这是元组中的第二个元素\(statusMessage)")
//如果你只想要一个元素或是某一个特定的元素则可以用下划线_标记你不想要的元素
let (name,_,profession,age) = xiaoming
println("这是小明的名字\(name)")
println("这是小明的年龄\(age)")
//元组可以作为返回值,当然也可以作为参数传递,元组的下标是从0开始的
//元组作为返回值返回
func daily(today:Int)->(name:String,age:Int,height:Double){
if today == 1{
return ("小明",20,1.78)
}else{
return ("小花",19,1.68)
}
}
var person1 = daily(1)
println("今天值日是\(person1.0)")
//元组作为参数值传参
let animal1 = ("dog","run",5)
let animal2 = ("snake","crawl",10)
func species(animal:(String,String,Int)) ->Void->Int{
func reptile()->Int{
println("这是爬行动物")
return 100
}
func mammal()->Int{
println("这是哺乳动物")
return 200
}
var way:String = animal.1
if equal(way, "run"){
return mammal
}else{
return reptile
}
}
var animalType = species(animal1)
var a = animalType()
println(a)
var animalType1 = species(animal2)
var b = animalType1()
println(b)