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

首頁(yè) > 編程 > Swift > 正文

Swift 3.0基礎(chǔ)學(xué)習(xí)之下標(biāo)

2020-03-09 17:46:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

類,結(jié)構(gòu)體和枚舉都可以定義下標(biāo),使用下標(biāo)可以快速訪問(wèn)集合,列表或者序列的數(shù)據(jù)成員元素。可以使用someArray[index]來(lái)訪問(wèn)Array, 使用someDictionary[key]來(lái)訪問(wèn)Dictionary。

一個(gè)類型可以定義多個(gè)下標(biāo)。

定義一個(gè)get set的下標(biāo):

subscript(index: Int) -> Int { get {  // return an appropriate subscript value here } set(newValue) {  // perform a suitable setting action here }}

定義一個(gè)read-only的下標(biāo)

subscript(index: Int) -> Int { // return an appropriate subscript value here}

例子:

struct TimesTable { let multiplier: Int subscript(index: Int) -> Int {  return multiplier * index }}let threeTimesTable = TimesTable(multiplier: 3)print("six times three is /(threeTimesTable[6])")// Prints "six times three is 18"

還可以使用多個(gè)下標(biāo), 任何類型,除了in-out類型的參數(shù)

struct Matrix { let rows: Int, columns: Int var grid: [Double] init(rows: Int, columns: Int) {  self.rows = rows  self.columns = columns  grid = Array(repeating: 0.0, count: rows * columns) } func indexIsValid(row: Int, column: Int) -> Bool {  return row >= 0 && row < rows && column >= 0 && column < columns } subscript(row: Int, column: Int) -> Double {  get {   assert(indexIsValid(row: row, column: column), "Index out of range")   return grid[(row * columns) + column]  }  set {   assert(indexIsValid(row: row, column: column), "Index out of range")   grid[(row * columns) + column] = newValue  } }}

參考翻譯英語(yǔ)原文:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html#//apple_ref/doc/uid/TP40014097-CH16-ID305

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者使用Swift能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到swift教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 萨嘎县| 大兴区| 霍邱县| 福清市| 静安区| 连云港市| 谢通门县| 巫山县| 怀来县| 元朗区| 惠来县| 临颍县| 北票市| 道孚县| 广东省| 巴彦县| 崇州市| 石屏县| 苏州市| 揭西县| 屏东市| 滨州市| 平武县| 葫芦岛市| 合川市| 孟村| 临西县| 永丰县| 苍梧县| 石首市| 武山县| 化州市| 定安县| 抚州市| 靖远县| 武汉市| 浦北县| 宜春市| 依安县| 巧家县| 蓬莱市|