使用Tuple打包数据

前端之家收集整理的这篇文章主要介绍了使用Tuple打包数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//(Int,String)
let success = (200,"Http Ok")  //(.0 200,.1 "Http Ok")
let fileNotFound:(Int,String) = (404,"File not found")    //(.0 404,.1 "File not found")
type(of:success)    //(Int,String).Type

success.0   //200
success.1   //"Http Ok"

//(String,Int,String)
let xiaoming = (name:"xiaoming",no:01,email:"11@1111.com")    //(.0 "xiaoming",.1 1,.2 "11@1111.com")

xiaoming.name   //"xiaoming"
xiaoming.no //1
xiaoming.email  //"11@1111.com"

//Tuple decomposition
var (successCode,successMessage) = success
successCode = 201   //不会影响success,只是把success的值放进模型内
successMessage  //"Http Ok"

success //(.0 200,.1 "Http Ok")

let (_,errorMessage) = fileNotFound    //不需要的参数可以用 _ 表示忽略
errorMessage    //"File not found"


//只有成员个数相同的Tuple能相互比较
let tuple11 = (1,1)
let tuple12 = (1,2)
tuple11 < tuple12   //true
tuple11 == tuple12  //false

let tuple123 = (1,2,3)
//tuple12 == tuple123 //错误,成员个数不相同

//另外成员个数超过6个的Tuple相互比较会编译错误
let tuple61 = (1,3,4,5,6)
let tuple62 = (1,6)
tuple61 == tuple62  //true

let tuple71 = (1,6,7)
let tuple72 = (1,7)
//tuple71 == tuple72 //错误

//参考自SwiftV课堂视频源码

猜你在找的Swift相关文章