国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Swift學(xué)習(xí)筆記(一):基礎(chǔ)

2019-11-14 20:14:25
字體:
供稿:網(wǎng)友

一、常量 & 變量

//常量let constantsTeam = 1//變量var variablesTeam = 2

  盡可能使用常量,這樣更清晰并且內(nèi)存更不容易腎虧。

二、顯示/隱式指定類型

//隱式let inferredTeam = 3//顯式let explicitTeam:Int = 4

三、字符串輸出

//通過/(變量或常量名)來引用組合字符串PRintln("/(inferredTeam) is bigger than /(variablesTeam)!")

四、類和方法

// 類使用class關(guān)鍵字聲明class TipCalculator {   // 在類中創(chuàng)建屬性  let total: Double  let taxPct: Double  let subtotal: Double   // 屬性必須在聲明時(shí)賦予一個(gè)初始值,或者在initializer里,否則必須聲明其為optional(這里使用的是initializer)  init(total:Double, taxPct:Double) {    self.total = total    self.taxPct = taxPct    subtotal = total / (taxPct + 1)  }   // 方法使用func關(guān)鍵字聲明,返回Double類型  func calcTipWithTipPct(tipPct:Double) -> Double {    return subtotal * tipPct  }   // 無返回值方法  func printPossibleTips() {    println("15%: /(calcTipWithTipPct(0.15))")    println("18%: /(calcTipWithTipPct(0.18))")    println("20%: /(calcTipWithTipPct(0.20))")  } } // 創(chuàng)建類實(shí)例對(duì)象,通過類實(shí)例對(duì)象調(diào)用類中的方法let tipCalc = TipCalculator(total: 33.25, taxPct: 0.06)tipCalc.printPossibleTips()

五、數(shù)組和循環(huán)

//1let possibleTipsInferred = [0.15, 0.18, 0.20]let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]//2for possibleTip in possibleTipsInferred {  println("/(possibleTip*100)%: /(calcTipWithTipPct(possibleTip))")}//3for i in 0..<possibleTipsInferred.count {  let possibleTip = possibleTipsInferred[i]  println("/(possibleTip*100)%: /(calcTipWithTipPct(possibleTip))")}

六、字典Dictionary

// 1func returnPossibleTips() -> [Int: Double] {   let possibleTipsInferred = [0.15, 0.18, 0.20]  let possibleTipsExplicit:[Double] = [0.15, 0.18, 0.20]   // 創(chuàng)建一個(gè)空字典  var retval = Dictionary<Int, Double>()  for possibleTip in possibleTipsInferred {    let intPct = Int(possibleTip*100)    // 賦值給字典    retval[intPct] = calcTipWithTipPct(possibleTip)  }  return retval }

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 板桥市| 运城市| 苍梧县| 德安县| 镇安县| 泗阳县| 布拖县| 彭泽县| 淳安县| 颍上县| 拉孜县| 娱乐| 安新县| 漳平市| 岳西县| 泰来县| 江源县| 沈阳市| 友谊县| 太谷县| 富源县| 宁津县| 峨山| 衡东县| 图木舒克市| 平塘县| 张北县| 中超| 安达市| 吉首市| 那曲县| 南召县| 西平县| 南充市| 曲松县| 荣成市| 昌江| 铜鼓县| 阿拉尔市| 南漳县| 岗巴县|