Collections Swift 3.0转换-集合中包含(:)

Collections Swift 3.0转换-集合中包含(:),collections,swift3,contains,Collections,Swift3,Contains,我正在尝试将项目从Swift 2.3转换为Swift 3 以下是来自集合的包含(:)的一些问题: extension Collection { subscript (safe index: Index) -> Iterator.Element? { return indices.contains(index) ? self[index] : nil } } 错误是缺少参数标签'where:'in call 我添加了,其中:,但现在出现了另一个错误: 无法将

我正在尝试将项目从Swift 2.3转换为Swift 3

以下是来自
集合的
包含(:)
的一些问题:

extension Collection {
    subscript (safe index: Index) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
错误是
缺少参数标签'where:'in call

我添加了
,其中:
,但现在出现了另一个错误:

无法将“Self.Index”类型的值转换为预期的参数类型“(\ux)throws->Bool”

从Swift 3.0语言指南来看,它似乎应该可以正常工作:

if favoriteGenres.contains("Funk") {
    print("I get up on the good foot.")
} else {
    print("It's too funky in here.")
}

在Swift 3中,
集合
索引
属性不是一个
集合
,而是一个
索引库
序列
。它没有
包含(:)
方法,但只有
包含(其中:)
方法

(来自生成的标题。)

两者都适用于简单阵列:

let arr = [1,2,3]
print(arr[safe: 3]) //->nil
print(arr[safe: 2]) //->Optional(3)

但是我不确定哪一个更安全。

在Swift 3中,
集合
索引
属性不是
集合
,而只是一个
索引基
序列
。它没有
包含(:)
方法,但只有
包含(其中:)
方法

(来自生成的标题。)

两者都适用于简单阵列:

let arr = [1,2,3]
print(arr[safe: 3]) //->nil
print(arr[safe: 2]) //->Optional(3)
但总的来说,我不确定哪个更安全

extension Collection
where Indices.Iterator.Element: Equatable, Index == Indices.Iterator.Element
{
    subscript (safe index: Indices.Iterator.Element) -> Iterator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
let arr = [1,2,3]
print(arr[safe: 3]) //->nil
print(arr[safe: 2]) //->Optional(3)