Swift 元组 Tuple

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)

posted on 2019-07-09 14:57  二狗你变了  阅读(150)  评论(0编辑  收藏  举报

导航