加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

10.6 Swift中subscripts基本语法

发布时间:2020-12-14 06:31:54 所属栏目:百科 来源:网络整理
导读:/** subscripts ( 下标 ) 访问对象中数据的快捷方式 所谓的下标脚本语法就是能够通过 实例[索引值]来访问实例中的数据 */ let array = [ 1 , 2 , 3 , 4 , 5 ] // 我们可以写成这个样子 let array1: [ Int ] = [ 1 , 3 ] let array2: Array Int = [ 1 , 3 ]

/**

subscripts (下标)

访问对象中数据的快捷方式

所谓的下标脚本语法就是能够通过 实例[索引值]来访问实例中的数据

*/

let array = [1,2,3,4,5]

// 我们可以写成这个样子

let array1: [Int] = [1,3]

let array2: Array<Int> = [1,3]

print(array[1]) // 实例对象 [索引] subscripts

let dict = ["1" : 1] // key:value,key hash

let dict1:Dictionary<String,Int> = ["1" : 1]

print(dict["1"])

/**

比如我也想构造一个结构体或者类

可以像以上这样通过实例下标的方式访问,

Swift是可以做到的。

按住 command键单击点击进入我们可以看到

public subscript(index: Int) -> Element // array


array.subscript(index)

public subscript(key: Key) -> Value? // dict

dict.subscript(key)

*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读