<分区>
我想进行有效性检查并快速返回数组中的适当值,如下面的函数 objectFromArr(at:)。
var arr = [10, 20, 30, 40]
func objectFromArr(at: Int) -> Int? {
return at < 0 || at >= arr.count ? nil : arr[at]
}
我不想使用函数。因为 swift Array 通常使用下标来获取对象。 所以,如果可能的话,我想覆盖下标。
@inlinable public subscript(index: Int) -> Element
to
override @inlinable public subscript(index: Int) -> Element?