下标脚本就是对一个东西通过索引,快速取值的一种语法,例如数组的a[0]。这就是一个下标脚本。通过索引0来快速取值。在Swift中,我们可以对
类(Class)、结构体(structure)和枚举(enumeration)中自己定义下标脚本的语法
一、常规定义
class Student{
var scores:Int[] = Array(count:5,repeatedValue:0)
subscript(index:Int) -> Int{
get{
return scores[index];
}
set{
scores[index] = newValue
}
}
}
var a = Student()
a[0] = 1
a[1] = 2
println("a[0]:\(a[0]),a[1]:\(a[1])”)
重点: