Tuple:[Swift]_技术成长笔记_新浪博客

Tuple:[Swift]


// 元组 元组中的元素可以是值,也可以是区间。另外,使用下划线(_)来匹配所有可能的值。-----------

let tup = (88,"eqi",["dds"])

var ss = tup.0


var tup2 = (name:"mac", age: 88,["dds"])

var name = tup2.0  // 或者

var name1 = tup2.name


// 分解

var tp = (404,"not found")

let (code, status) = tp

let (code2,_) = tp  // _忽略值



func format(tup:(String, Int)) -> String {

    return "Hi,\(tup.0),\(tup.1)"

}




let somePoint = (1, 1)



switch somePoint {

case (0, 0):

    println("(0, 0) is at the origin")

case (_, 0):

    println("(\(somePoint.0), 0) is on the x-axis")

case (0, _):

    println("(0, \(somePoint.1)) is on the y-axis")

case (-2...2, -2...2):

    println("(\(somePoint.0), \(somePoint.1)) is inside the box")

default:

    println("(\(somePoint.0), \(somePoint.1)) is outside of the box")

}




let anotherPoint = (2, 0)

switch anotherPoint {

case (let x, 0):

    println("on the x-axis with an x value of \(x)")

    

case (0, let y):

    println("on the y-axis with a y value of \(y)")

case let (x, y):

    println("somewhere else at (\(x), \(y))")

}


// case 分支的模式可以使用where语句来判断额外的条件。

let yetAnotherPoint = (1, -1)

switch yetAnotherPoint {

case let (x, y) where x == y:

    println("(\(x), \(y)) is on the line x == y")

case let (x, y) where x == -y:

    println("(\(x), \(y)) is on the line x == -y")

case let (x, y):

    println("(\(x), \(y)) is just some arbitrary point")

}

// 输出 "(1, -1) is on the line x == -y"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值