我有一个使用NSIndexPath作为密钥的字典。它适用于Swift 2,但我找不到一个解决方案,使它适用于Swift 3.我不想使用String作为键,所以请不要建议它。
// init
fileprivate var cachedCellSizes: [NSIndexPath: CGSize] = [:]
// get value
if let size = cachedCellSizes[indexPath] {
return size
}
编译错误:
Ambiguous reference to member 'subscript'
我尝试了一些但不起作用的解决方案:
if let size:CGSize = cachedCellSizes[indexPath] as? CGSize {
return size
}
if let size:CGSize = cachedCellSizes[indexPath] as! CGSize {
return size
}
if let size:CGSize = cachedCellSizes["indexPath"] as CGSize {
return size
}
答案 0 :(得分:1)
fileprivate var cachedCellSizes: [NSIndexPath: CGSize] = [:]
if let size = cachedCellSizes[indexPath as NSIndexPath] {
print(indexPath)
}
OR
fileprivate var cachedCellSizes: [IndexPath: CGSize] = [:]
if let size = cachedCellSizes[indexPath] {
print(indexPath)
}