Swift 元组

2022-05-11  本文已影响0人  诗颜语
    // 元组  没有类型名 表示类型使用形如(String, Int)  只能用()  表达值也是,并且不限制类型,看下边例子
//    let tuple3 : (String, Int) = ("time", 20210811)
//    let (key1, value1) : (String, Int) = ("time", 20210811)
     
    let (key, value) = ("time", 20210811)
    print(key, value) // time 20210811
 
 
     
    let (a, b) = (key: "time", value: 20210811) // 可以添加自定义index,此示例的  key  value 便是
    print(a, b) // time 20210811
     
    let tuple = (key: "time", value: 20210811)
    print(tuple, tuple.key, tuple.value) // (key: "time", value: 20210811) time 20210811   使用自定义index访问
     
    func testTuple () -> (String, Int){
        return (name: "baidu", age: 21)
    }
     
    let tuple1 = testTuple()
    print(tuple1, tuple1.0, tuple1.1) // ("baidu", 21) baidu 21
     
    // 最常用的用法就是 接收函数返回值,可以模拟实现返回多个值
    let (name, age) = testTuple() // baidu 21
    print(name, age)
 
 
    let (n, m) = testTuple() // baidu 21
    print(n, m)
上一篇下一篇

猜你喜欢

热点阅读