Swift 元组 Tuple

<style></style> <style></style>

let infoArray:[Any] = ["jack",18,1.88]

let infoName=infoArray[0] as!String //此处为Any类型强转为String类型

print(infoName.count)

 

 

let infoDict:[String:Any]=["name":"jack","age":18]

let dictName=infoDict["name"] as! String //此处为Any类型强转为String类型

print(dictName.count)

 

//使用数组或者字典取出的类型为Any类型

//使用元组保存信息,能头准确的推导出数据类型

//写法一:

let infoTuple1=("jack",18,1.88)

let tupleName=infoTuple1.0

let tupleAge = infoTuple1.1

let tupleHeight = infoTuple1.2

 

//写法二:使用别名

let infoTuple2=(name:"jack",age:18,height:1.88)

print(infoTuple2.name)

print(infoTuple2.age)

 

//写法三:

let (name,age,height) = ("jack",18,1.88)

print(name)

上一篇:jack -复习for循环,学习随机数


下一篇:JavaScript高级知识