美文网首页
Swift5.1学习随笔之元组tuple

Swift5.1学习随笔之元组tuple

作者: SAW_ | 来源:发表于2020-04-10 14:18 被阅读0次

    元组是多个数据类型组合在一起
    举个例子🌰

    let http404Error = (404, "Not Found")
    print("The status code is \(http404Error.0)")
    //获取元组的内部数据,通过位置索引
    let value1 = http404Error.0 // 404
    let value2 = http404Error.1 // "Not Found"
    

    这些也是一个元组

    let (statusCode, statusMessage) = http404Error 
    print("The status code is \(statusCode)")
    print("The status message is \(statusMessage)")
    
    //可以用_省略不需要的值
    let (statusCode, _) = http404Error 
    print("The status code is \(statusCode)")
    
    //可以像字典那样设置元组里面的值
    let http404Error = (statusCode: 404, description: "Not Found")
    print("The status code is \(http404Error.statusCode)")
    

    相关文章

      网友评论

          本文标题:Swift5.1学习随笔之元组tuple

          本文链接:https://www.haomeiwen.com/subject/yudrmhtx.html