var dict = Dictionary<Int64, ExternalInfo>()
为上面的字典创建一个扩展名
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'
最佳答案
尝试使用SignedIntegerType
代替IntegerLiteralConvertible
,这是不同整数类型之间更好的通用协议(protocol):
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))
}
}
关于swift - 错误 : Cannot subscript a value of 'Dictionary with an index of type ' Int64',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39315624/