2016-09-04 6 views
0
var dict = Dictionary<Int64, ExternalInfo>() 

エラー:「タイプのインデックスを持つ辞書 『の値を添字ことはできませんInt64の』

extension Dictionary where Key: IntegerLiteralConvertible, Value: ExternalInfo { 

    func contains(id: Int64) -> Bool { 
     return self[id] != nil 
     /* return self[3] != nil */ // No issue 
    } 

    mutating func remove(id: Int64) { 
     removeValueForKey(id) 
    } 
} 

のような上記の辞書の拡張子を作成しますこれは、両方のステートメントのためのいくつかのコンパイラレベルのエラーを投げます。何をする必要がありますか?

Cannot subscript a value of 'Dictionary with an index of type 'Int64'

答えて

1

代わりIntegerLiteralConvertibleで、これは異なる整数型の間のより良い一般的なプロトコルであるSignedIntegerTypeを使用してみてください:

extension Dictionary where Key: SignedIntegerType, Value: ExternalInfo { 

    func contains(id: Int64) -> Bool { 
     return self[Key(id)] != nil 
    } 

    mutating func remove(id: Int64) { 
     removeValueForKey(Key(id)) 
    } 
} 
関連する問題