简体   繁体   中英

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

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.

Try like this.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM