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.

Community
  • 1
  • 1
Aldo
  • 13
  • 8
  • you have 3 subscripts for [String:AnyObject]. Is is supposed to be [String:[AnyObject]] ? – Shades Sep 17 '16 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 Sep 17 '16 at 10:42

2 Answers2

2

Try like this.

let dictperdataselezionata = NSDictionary(contentsOfFile: pathperdataselezionata!) as! [String:[String:[String:AnyObject]]]
let valoridataodierna = dictperdataselezionata[annoscelto]?[mesescritto]?![daymonth?] as? [Double]
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • This has solved my problem. Thanks a lot. Due my reputation I can't accept the answer. – Aldo Sep 17 '16 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]
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117