swift 元组的使用

最近在定义方法参数的时候用到元祖,当然也可以用数组或字典代替(但是没有想要的提示用户输入类等的效果)所以发现元祖还是很好用,废话不多说 附上自己的一个列子然后加上一些资料

 func GJ_contentOffsetConstraint(subView:UIView,superView:UIView,constant:(left:CGFloat,top:CGFloat,right:CGFloat,bottom:CGFloat))

{

//内容省略

}


//使用的时候的效果 这边调用这个方法

//赋值前的样子当然有一层灰的  感觉这样开发者体验比较好 更容易读懂所以 这边推荐给大家

 constraint.GJ_contentOffsetConstraint(view1, superView: view2, constant: (left: CGFloat, top: CGFloat, right: CGFloat, bottom: CGFloat))

//赋值后的样子

        constraint.GJ_contentOffsetConstraint(view1, superView: view2, constant: (100,150,-100,-230))


//下面给大家转载个写的比较好的文章 

元组

元组(tuples)把多个值组合成一个复合值。元组内的值可以使任意类型,并不要求是相同类型。


let http404Error = (404, "Not Found")
// http404Error 的类型是 (Int, String),值是 (404, "Not Found")
你可以将一个元组的内容分解(decompose)成单独的常量和变量,然后你就可以正常使
用它们了:

let (statusCode, statusMessage) =  (404, "Not Found")
 println("The status code is \(statusCode)")
// 输出 "The status code is 404"
 println("The status message is \(statusMessage)")5.
 // 输出 "The status message is Not Found"
如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记:
 let (justTheStatusCode, _) = (404, "Not Found")
 println("The status code is \(justTheStatusCode)")
3. // 输出 "The status code is 404"
此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:

let http404Error = (404, "Not Found")

 println("The status code is \(http404Error.0)")
 // 输出 "The status code is 404"
 println("The status message is \(http404Error.1)")
 // 输出 "The status message is Not Found"
你可以在定义元组的时候给单个元素命名:

let http200Status = (statusCode: 200, description: "OK")
给元组中的元素命名后,你可以通过名字来获取这些元素的值:

 println("The status code is \(http200Status.statusCode)")
 // 输出 "The status code is 200"
 println("The status message is \(http200Status.description)")
 // 输出 "The status message is OK"
原文地址http://www.jianshu.com/p/42255053e906




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值