subscripts(下标):
访问对象中数据的快捷方式
所谓下标脚本语法就是能够通过, 实例[索引值]来访问实例中的数据
类似于以前我们访问数字和字典, 其实Swift中的数组和字典就是一个结构体
struct Student {
var name:String = "qbs"
var math:Double = 99.0
var chinese:Double = 99.0
var english:Double = 99.0
func score(course:String) ->Double?
{
switch course{
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
var chinese:Double = 99.0
var english:Double = 99.0
func score(course:String) ->Double?
{
switch course{
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
要想实现下标访问, 必须实现subscript方法
如果想要通过下标访问, 必须实现get方法
如果想要通过下表赋值, 必须实现set方法
subscript(course:String) ->Double?{
get{
switch course{
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
set{
switch course{
get{
switch course{
case "math":
return math
case "chinese":
return chinese
case "english":
return english
default:
return nil
}
}
set{
switch course{
case "math":
因为返回的是可选类型
math = newValue!
case "chinese":
chinese = newValue!
case "english":
english = newValue!
default:
print("not found")
}
}
}
}
case "chinese":
chinese = newValue!
case "english":
english = newValue!
default:
print("not found")
}
}
}
}
var stu = Student(name: "zs",
math: 99.0,
chinese: 88.0,
english: 10.0)
print(stu.score("math"))
输出结果: Optional(99.0)
stu["chinese"] = 100.0
print(stu["chinese"])
输出结果: Optional(100.0)