1

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.

2
  • you have 3 subscripts for [String:AnyObject]. Is is supposed to be [String:[AnyObject]] ?
    – Shades
    Commented Sep 17, 2016 at 10:31
  • 1
    The compiler needs to know the types of all subscripted objects. And since the data comes from a file in the bundle why do you use question marks? If you get a runtime error your design is very bad.
    – vadian
    Commented Sep 17, 2016 at 10:42

2 Answers 2

2

Try like this.

let dictperdataselezionata = NSDictionary(contentsOfFile: pathperdataselezionata!) as! [String:[String:[String:AnyObject]]]
let valoridataodierna = dictperdataselezionata[annoscelto]?[mesescritto]?![daymonth?] as? [Double]
1
  • This has solved my problem. Thanks a lot. Due my reputation I can't accept the answer.
    – Aldo
    Commented Sep 17, 2016 at 10:47
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.