swift - 错误 : Cannot subscript a value of 'Dictionary with an index of type ' Int64'

标签 swift dictionary

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/

相关文章:

swift - 您需要帮助在连接到步进器的标签中存储数字

ios - 使用 cocoapods 的 PayPal-iOS-SDK 在桥接头中抛出错误

swift - 理解 try catch , swift3 xcode

ios - 在swift 5中选择下拉菜单后如何隐藏表格单元格?

C# 将静态类放入字典中

python - 将列表的列表转换为字典

ios - 如何使用方案更改 ui 元素的颜色? (Xcode, swift )

c++ - 使用字符串和枚举映射时,Switch执行第一种情况而不是默认情况

python - 整理 csv 数据并计算平均成绩

python - 如何将带有嵌套字典的列表写入 csv 文件?