阅读811 返回首页    go 阿里云 go 技术社区[云栖]


Swift下标

还记得字典吗?


var numberOfLegs= ["spider": 8,"ant": 6, "cat":4]
numberOfLegs["bird"]= 2


["bird"]就是下标 下标可以在类和结构体中定义。

定义下标


subscript(index: Int) -> Int {
get {
//return anappropriate subscript value here
}
set(newValue) {
// perform a suitable settingaction here
}
}

 

 

只读下标


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


实例:


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交流讨论论坛论坛:https://www.cocoagame.net

欢迎加入Swift技术交流群:362298485



最后更新:2017-04-03 06:03:10

  上一篇:go Cortex-M3
  下一篇:go 移动App该如何保存用户密码