下标脚本(Subscripts)
someArray[index]
,访问字典(Dictionary)实例中的元素可以这样写 someDictionary[key]
。下标脚本语法
subscript(index: Int) -> Int {
get {
// 返回与入参匹配的Int类型的值
}
set(newValue) {
// 执行赋值操作
}
}
struct Matix {
var arrList = [Int]();
init(arrList: [Int]){
self.arrList = arrList;
}
func indexValidRow(index: Int) ->Bool{
return index < arrList.count;
}
subscript(index: Int) ->Int{
get {
assert(indexValidRow(index), "Index out of range");
return arrList[index];
}
set {
assert(indexValidRow(index), "Index out of range");
arrList[index] = newValue;
}
}
}