Swift 3 : Error: Type 'Any' has no subscript member

岁酱吖の 提交于 2020-01-11 11:48:08

问题


I updated to Swift 3 and I get this error and I can't solve it.

Type 'Any' has no subscript member

I already read the answers:

39480150 - 38956785 - 39516199

But I couldn't solve my problem with the answers.

This is my code:

let pathperdataselezionata = Bundle.main.path(forResource: "Annuale", ofType: "plist")
let dictperdataselezionata = NSDictionary(contentsOfFile: pathperdataselezionata!) as![String:AnyObject]
let valoridataodierna = dictperdataselezionata[annoscelto]?[mesescritto]?![daymonth?] as? [Double]
let Grad = Int(valoridataodierna![0])
let Ampo:Double = valoridataodierna![1]

And I get the error on the line:

let valoridataodierna

Any help is really appreciated.

Thanks.


回答1:


Try like this.

let dictperdataselezionata = NSDictionary(contentsOfFile: pathperdataselezionata!) as! [String:[String:[String:AnyObject]]]
let valoridataodierna = dictperdataselezionata[annoscelto]?[mesescritto]?![daymonth?] as? [Double]



回答2:


It looks like Apple's recommendation is to use lots of variables, see: Working with JSON in Swift.

if let pathperdataselezionata = Bundle.main.path(forResource: "Annuale", ofType: "plist"),
    let dictperdataselezionata = NSDictionary(contentsOfFile: pathperdataselezionata) as? [String: Any],
    let dictAnnoscelto = dictperdataselezionata[annoscelto] as? [String: Any],
    let dictMesescritto = dictAnnoscelto[mesescritto] as? [String: Any],
    let daymonth = daymonth,
    let valoridataodierna = dictMesescritto[daymonth] as? [Double]
{
    let Grad = Int(valoridataodierna[0])
    let Ampo:Double = valoridataodierna[1]
}


来源:https://stackoverflow.com/questions/39545622/swift-3-error-type-any-has-no-subscript-member

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