Swift 3 Type 'Any' has no subscript members

99封情书 提交于 2019-12-21 04:07:09

问题


I just converted my project to Swift 3 I have this line of code here:

let type = self.data[indexPath.row]["Type"] as? String

but now I get this error:

Type 'Any' has no subscript members

Why am I getting this error and do I fix it?


回答1:


let type = (self.data[indexPath.row] as? [String : String])?["Type"]

You need to cast self.data[indexPath.row] to a dictionary.




回答2:


Either your data or the value returned when you subscript it, e.g. data[0] has the Any type, which you're trying to subscript.

Make sure that the compiler knows that whatever you get is a known type which supports subscripting. Like and array or dictionary for example.




回答3:


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 {
                }
}

Then it compiled without error.



来源:https://stackoverflow.com/questions/39778940/swift-3-type-any-has-no-subscript-members

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!