如果我在Swift中有一个数组,并尝试访问一个越界的索引,有一个不足为奇的运行时错误:

var str = ["Apple", "Banana", "Coconut"]

str[0] // "Apple"
str[3] // EXC_BAD_INSTRUCTION

然而,我本以为有了Swift带来的所有可选的链接和安全性,做这样的事情是微不足道的:

let theIndex = 3
if let nonexistent = str[theIndex] { // Bounds check + Lookup
    print(nonexistent)
    ...do other things with nonexistent...
}

而不是:

let theIndex = 3
if (theIndex < str.count) {         // Bounds check
    let nonexistent = str[theIndex] // Lookup
    print(nonexistent)   
    ...do other things with nonexistent... 
}

但事实并非如此——我必须使用ol' if语句来检查并确保索引小于str.count。

我尝试添加我自己的下标()实现,但我不确定如何将调用传递给原始实现,或者访问项目(基于索引)而不使用下标符号:

extension Array {
    subscript(var index: Int) -> AnyObject? {
        if index >= self.count {
            NSLog("Womp!")
            return nil
        }
        return ... // What?
    }
}

当前回答

说实话,我也遇到过这个问题。从性能的角度来看,Swift数组应该能够抛出。 让x = try a[y] 这很好,也可以理解。

其他回答

因为数组可以存储nil值,如果数组[index]调用越界,返回nil是没有意义的。 因为我们不知道用户想如何处理越界问题,所以使用自定义操作符没有意义。 相反,使用传统的控制流来展开对象并确保类型安全。

if let index = array.checkIndexForSafety(index:Int) let item = array[safeIndex: index] if let index = array.checkIndexForSafety(index:Int) array[safeIndex: safeIndex] = myObject

extension Array {

    @warn_unused_result public func checkIndexForSafety(index: Int) -> SafeIndex? {

        if indices.contains(index) {

            // wrap index number in object, so can ensure type safety
            return SafeIndex(indexNumber: index)

        } else {
            return nil
        }
    }

    subscript(index:SafeIndex) -> Element {

        get {
            return self[index.indexNumber]
        }

        set {
            self[index.indexNumber] = newValue
        }
    }

    // second version of same subscript, but with different method signature, allowing user to highlight using safe index
    subscript(safeIndex index:SafeIndex) -> Element {

        get {
            return self[index.indexNumber]
        }

        set {
            self[index.indexNumber] = newValue
        }
    }

}

public class SafeIndex {

    var indexNumber:Int

    init(indexNumber:Int){
        self.indexNumber = indexNumber
    }
}

你可以试试

if index >= 0 && index < array.count {
    print(array[index]) 
}

适用于Swift 2

尽管这个问题已经被回答过很多次了,但我想给出一个更符合Swift编程时尚走向的答案,用Crusty的话来说就是:“先考虑协议”。

• What do we want to do? - Get an Element of an Array given an Index only when it's safe, and nil otherwise • What should this functionality base it's implementation on? - Array subscripting • Where does it get this feature from? - Its definition of struct Array in the Swift module has it • Nothing more generic/abstract? - It adopts protocol CollectionType which ensures it as well • Nothing more generic/abstract? - It adopts protocol Indexable as well... • Yup, sounds like the best we can do. Can we then extend it to have this feature we want? - But we have very limited types (no Int) and properties (no count) to work with now! • It will be enough. Swift's stdlib is done pretty well ;)

extension Indexable {
    public subscript(safe safeIndex: Index) -> _Element? {
        return safeIndex.distanceTo(endIndex) > 0 ? self[safeIndex] : nil
    }
}

不正确,但它给出了一个概念

斯威夫特4

为那些更喜欢传统语法的人提供的扩展:

extension Array {
    func item(at index: Int) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

基于Nikita Kukushkin的回答,有时候你需要安全地赋值给数组下标,也需要从它们中读取。

myArray[safe: badIndex] = newValue

因此,这里是对Nikita的答案(Swift 3.2)的更新,通过添加safe:参数名,也允许安全写入可变数组索引。

extension Collection {
    /// Returns the element at the specified index if it is within bounds, otherwise nil.
    subscript(safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

extension MutableCollection {
    subscript(safe index: Index) -> Element? {
        get {
            return indices.contains(index) ? self[index] : nil
        }

        set(newValue) {
            if let newValue = newValue, indices.contains(index) {
                self[index] = newValue
            }
        }
    }
}