Swift学习笔记(二):属性、元组

一、属性的getter和setter

//设置计算型属性:其并不真正的存储值,而是每次通过其他值计算得来
var subtotal: Double {
  //getter:通过total、taxPct计算获得total的值  
  get {
    return total / (taxPct + 1)
  }
  //setter:更新的是相关的值(比如此处基于newSubtotal来设置total、taxPct的值)
  set(newSubtotal) { 
     //... 
  }
}

二、元组 | Tuples

//创建一个unamed tuples
let tipAndTotal = (4.00, 25.19)
//创建一个named tuples
let tipAndTotalNamed = (tipAmt:4.00, total:25.19)
tipAndTotalNamed.tipAmt
tipAndTotalNamed.total
//单行创建tuples
let tipAndTotalNamed:(tipAmt:Double, total:Double) = (4.00, 25.19)

  返回元组类型

let total = 21.19
let taxPct = 0.06
let subtotal = total / (taxPct + 1)
//这里返回的是元组的类型
func calcTipWithTipPct(tipPct:Double) -> (tipAmt:Double, total:Double) {
  let tipAmt = subtotal * tipPct
  let finalTotal = total + tipAmt
  return (tipAmt, finalTotal)
}
calcTipWithTipPct(0.20)

三、

转载于:https://www.cnblogs.com/Evan-You/p/3853704.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值