GoZ*_*ner 15
定义你自己的:
extension Array {
func ref (i:Int) -> T? {
return 0 <= i && i < count ? self[i] : nil
}
}
Run Code Online (Sandbox Code Playgroud)
该ref()函数返回一个可选项,因此它可以是nil,并且您可以使用该if let语法从ref()存在时访问返回的值.您可以这样使用:
var myA = [10,20,30]
if let val = myA.ref(index) {
// Use 'val' if index is < 3
}
else {
// Do this if the index is too high
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4556 次 |
| 最近记录: |