Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list or sequence. 类、结构体、枚举都可以定义脚注(不知道这里应该怎么翻译scripts, 姑且当做是脚注吧。举个例子来说脚注也就是someArray[3]中的中括号和里面的数字3)

You can define multiple subscripts for a single type, and the appropriate subscript overload to use is selected based on the type of index value you pass to the subscript. 也就是说脚注可以重载,编译器会根据传入的参数不同选择不同的脚注

// basic syntax 基本语法
subscript(intdex: Int) -> Int {get {// return an appropriate subscript value here}set(newValue) {// perform a suitable setting action here}
}// as with read-only computed properties, you can drop the get keyword for read-pnly subscripts
// 为了实现只读subscript,你可以省去get参数
struct TimesTable {let multiplier: Intsubscript(index: Int) -> Int {return multiplier * index}
}

Subscript Options

Subscripts can take any number of input parameters, and these input parameters can be of any type. subscript可以有任何数量的参数,并且参数可以是任何类型

就像下面的Matrix定义,我们可以很轻松的写出

var m = Matrix(rows:3, columns:3)

let p = m[1,2]

m[2,2] = p之类的语句

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

Subscripts相关推荐

  1. Error in **: incorrect number of subscripts on matrix

    Error in **: incorrect number of subscripts on matrix 目录 Error in **: incorrect number of subscripts ...

  2. [翻译] [LaTeX] 上标和下标 - Subscripts and superscripts

    原  文:Subscripts and superscripts 译  者:Xovee 翻译时间:2020年6月18日 上标和下标 在数学表达式中,上下标的使用是非常普遍的,例如各种指数.索引.特殊的 ...

  3. R语言ggplot2可视化:ggplot2可视化为轴标签添加复杂下标(Subscripts)和上标(superscripts)、离子化学符号(ionic chemical notation)等

    R语言ggplot2可视化:ggplot2可视化为轴标签添加复杂下标(Subscripts)和上标(superscripts).离子化学符号(ionic chemical notation)等 目录

  4. pytorch JIT浅解析

    概要   Torch Script中的核心数据结构是ScriptModule. 它是Torch的nn.Module的类似物,代表整个模型作为子模块树. 与普通模块一样,ScriptModule中的每个 ...

  5. matlab getstart,matlab帮助文件(matlab get start.pdf)

    [实例简介]Matlab Primer [实例截图] [核心代码] Quick Start 1 MATLAB Product Description ......................... ...

  6. 图像处理和图像识别中常用的matlab函数

    下面仅给出函数的大概意思,详细用法见: help  函数名 或 matlab help 1.imread:read image from graphics file: 2.imshow:display ...

  7. UNICODE字符集表

    注:中文范围 4E00-9FBF:CJK 统一表意符号 (CJK Unified Ideographs) 0000-007F:C0控制符及基本拉丁文 (C0 Control and Basic Lat ...

  8. matplotlib绘图库入门

    2019独角兽企业重金招聘Python工程师标准>>> matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图.而且 ...

  9. matlab 将矩阵中的0置为Inf(邻接矩阵)

    矩阵中的0置为Inf(邻接矩阵) ind=find(x==0); x(ind)=inf; index=isnan(z); z(index)=0; 在Matlab中,有一个logical数据类型,和C+ ...

最新文章

  1. 计算机四级网络工程师考试重点
  2. 通过apt自动生成建造者模式单线程版代码(三)
  3. CSS3运算 calc()函数是怎么实现计算
  4. Angular 项目里 angular.json 文件内容的学习笔记
  5. Vh和Vw的简介和使用
  6. java升级菜单切换_java 关于系统菜单升级
  7. OpenLayers项目分析——(一)项目介绍
  8. 检查java_如何检查Java版本?
  9. Hadoop Trash回收站使用指南
  10. openoffice转换pdf 异常问题查找处理 errorCode 525
  11. P3698 [CQOI2017]小Q的棋盘
  12. 1月10日云栖精选夜读:专访金榕:四年蜕变,阿里iDST是如何登上浪潮之巅的?...
  13. SAP ABAP开发实战——从入门到精通系列教程目录
  14. 贝叶斯网络(概率图模型)
  15. Activiti6--入门学习--中间事件
  16. PHP公历农历转换(阴历阳历转换)阴历和阳历转换
  17. 获取微信运动 php,微信运动数据抓取(PHP语言)
  18. (十)苏世民:我的经验和教训:掌控(1~6)
  19. Codeforces Round #486 (Div. 3)
  20. ftp上传文件 严重文件传输错误

热门文章

  1. 正则表达式学习和正则表达式的使用(邮箱检测)
  2. 利用python编写一个pc模拟器明日方舟脚本_明日方舟脚本1.0(python\adb\cv2)
  3. 公司取名注意事项(如何取到合适的公司名)
  4. python搞笑语句_云计算开发学习笔记:Python3 import语句
  5. AE 视频画面变白记录
  6. 腾讯开年连投两笔,全球数字银行爆发正当时?
  7. 生日祝福 创意C语言代码
  8. 安卓手机安装texlive方法
  9. Perl:正则表达式
  10. 分享9款漂亮的浪漫情侣网站模板