2016-10-16 8 views
0

私はスウィフト2.3からスウィフト3.ここでスウィフト3.0変換が

にプロジェクトを変換しようとしていますが、Collectionからcontains(_:)といくつかの問題であるコレクションに_ :)(含まれてい私はwhere:を追加しましたが、今、別のエラーが表示されますMissing argument label 'where:' in call

です:

エラーなしで動作するはずと思われスウィフト3.0言語のガイドから

:スウィフト3では

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

答えて

2

CollectionindicesプロパティがCollectionではなく、ただIndexableBase & Sequencecontains(_:)メソッドはありませんが、contains(where:)メソッドのみです。 (。生成されたヘッダから)

associatedtype Indices : IndexableBase, Sequence = DefaultIndices<Self> 

public var indices: Self.Indices { get } 

あなたはこのような何か書く必要がある場合があります。あなたが追加して、Sequence where Iterator.Element : Equatableためcontains(_:)メソッドを呼び出すことができます

extension Collection { 
    subscript (safe index: Index) -> Iterator.Element? { 
     return (startIndex..<endIndex).contains(index) ? self[index] : nil 
    } 
} 

さもをいくつかの制約:

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) 

しかし、私は、一般的に安全であるか分かりません。

関連する問題