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