-3
    if let name = property["Name"] as? String,
      let latitude = property["Latitude"] as? NSNumber,
      let longitude = property["Longitude"] as? NSNumber,
      let image = property["Image"] as? String {

      // To learn how to read data from plist file, check out our Saving Data
      // in iOS Video Tutorial series.

      // Code goes here


    }

When I converted my code to current swift version, I am facing 'Type any has no subscript error'. I am fairly new to swift and stuck at this point. Saw other related posts, but no where I could find a solution for this. Please help.

  • [Edit] your code showing how `property` is declared and initialized. – rmaddy Apr 22 '17 at 01:34
  • Possible duplicate of [Type 'Any' has no subscript members after updating to Swift 3](http://stackoverflow.com/questions/39480150/type-any-has-no-subscript-members-after-updating-to-swift-3) – l'L'l Apr 22 '17 at 01:35
  • 1
    And certainly your answer can be found amongst [these search results](http://stackoverflow.com/search?q=%5Bswift%5D+Type+any+has+no+subscript). – rmaddy Apr 22 '17 at 01:35
  • 1
    ... also note: http://stackoverflow.com/q/38956785/499581, http://stackoverflow.com/q/39778940/499581, http://stackoverflow.com/q/39576282/499581, http://stackoverflow.com/q/39549107/499581 – l'L'l Apr 22 '17 at 01:39

1 Answers1

1

I poured over the many questions with duplicate titles and couldn't find one that clearly covered your case, so I'm going to explain what is going on.

Your variable property is clearly declared as type Any, which is a variable that can hold any type. The error message is telling you that you can't use subscripting [] with it. It is clear that you believe that it is a Dictionary with a key that is a String and a value that can be anything. In Swift 3, anything is typed Any. So your property is expected to be [String : Any]. If you cast property to [String : Any], then you will be able to use [] with it.

The first thing you should do is attempt to cast property to [String : Any] and then proceed if that works:

if let property = property as? [String : Any],
    let name = property["Name"] as? String,
    let latitude = property["Latitude"] as? NSNumber,
    let longitude = property["Longitude"] as? NSNumber,
    let image = property["Image"] as? String {

    // To learn how to read data from plist file, check out our Saving Data
    // in iOS Video Tutorial series.

    // Code goes here
}
Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294