首页 > 代码库 > Swift下标
Swift下标
还记得字典吗?
[html] view plaincopy
- var numberOfLegs= ["spider": 8,"ant": 6, "cat":4]
- numberOfLegs["bird"]= 2
["bird"]就是下标 下标可以在类和结构体中定义。
定义下标
[html] view plaincopy
- subscript(index: Int) -> Int {
- get {
- //return anappropriate subscript value here
- }
- set(newValue) {
- // perform a suitable settingaction here
- }
- }
只读下标
[html] view plaincopy
- subscript(index: Int) -> Int {
- //return anappropriate subscript value here
- }
实例:
[html] view plaincopy
- struct TimesTable {
- let multiplier: Int
- subscript(index: Int) ->Int {
- return multiplier * index
- }
- }
- let threeTimesTable = TimesTable(multiplier: 3)
- println("sixtimes three is \(threeTimesTable[6])")
- // prints"six times three is 18"
Swift交流讨论论坛论坛:http://www.cocoagame.net
欢迎加入Swift技术交流群:362298485
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。