【发布时间】:2016-09-29 19:56:57
【问题描述】:
我刚刚将我的项目转换为 Swift 3 我这里有这行代码:
let type = self.data[indexPath.row]["Type"] as? String
但现在我收到此错误:
Type 'Any' has no subscript members
为什么我会收到此错误,我应该修复它吗?
【问题讨论】:
我刚刚将我的项目转换为 Swift 3 我这里有这行代码:
let type = self.data[indexPath.row]["Type"] as? String
但现在我收到此错误:
Type 'Any' has no subscript members
为什么我会收到此错误,我应该修复它吗?
【问题讨论】:
let type = (self.data[indexPath.row] as? [String : String])?["Type"]
您需要将self.data[indexPath.row] 转换为字典。
【讨论】:
您的data 或下标时返回的值,例如data[0] 具有 Any 类型,您正在尝试对其下标。
确保编译器知道你得到的任何东西都是支持下标的已知类型。例如,like 和数组或字典。
【讨论】:
Even I was facing the same error
for item in 0...((currentSectionCells as! NSMutableArray).count - 1) {
if currentSectionCells[item]["isVisible"] as! Bool == true {
} // error "Type 'Any' has no subscript "
}
Then changed to code as below
for item in 0...((currentSectionCells as! NSMutableArray).count - 1) {
if (item as! NSDictionary).value(forKey: "isVisible") as! Bool == true {
}
}
然后编译没有错误。
【讨论】: