- 元组的声明
var point = ( 5, 2 )
var httpResponse = ( 404 , "Not Found" )
point.0
point.1
输出结果
5
2
- 元组解包
let ( x , y ) = point2
print(x)
print(y)
输出结果
5
2
- 元组的分量
point.0
point.1
输出结果
5
2
- 命名元组分量
let point3 = ( x: 2 , y: 1 )
point3.x
point3.y
let point4: (x:Int , y:Int) = (3,4)
point4.x
point4.y
输出结果
2
1
3
4
- 使用_忽略元组分量
let loginResult = ( true , "Swift_Tuple" )
let ( isLoginSuccess , _ ) = loginResult
if isLoginSuccess{
print("Login success!")
}
else{
print("Login failed!")
}
网友评论