-1

I want to iterate array of dictionary but it gives error that "Value of type 'Any' has no subscripts". I just followed answer provide in link

Here is the array of dictionaries in debug mode: ScreenShotOfArray

Here is how I'm trying to do it:

if let markersArray = appDelegate.markersArray{
            for marker in markersArray{
                let name = marker["name"] as? [String:Any]
            }
        }

Also tried this way but still doesn't work:

if let markersArray = appDelegate.markersArray{
                for marker in markersArray{
                    let name = marker["name"]
                }
            }

Here is how 'marker' looks in debug mode: markerScreenShot

When I print marker, it prints the right content but unable to extract the data.

Any help will be really appreciated and already tried : link

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Haroon khan
  • 1,018
  • 2
  • 15
  • 27

1 Answers1

0

So i think your array consist of [Any] and will therefor not be able to use string key as it doesn't know it's a dictionary:

let array: [Any] = [["test": 12], ["testing": 2], ["12": 2]]
    for item in array {
        if let item = item as? [String: Any] {
            let name = item["test"]
        }
    }
Vollan
  • 1,887
  • 11
  • 26
  • Why don't you cast the array itself if all elements are probably dictionaries – Leo Dabus Aug 19 '19 at 12:30
  • @LeoDabus As the array is declared as `Any` i made a wild guess that it can have other value types as well. representing an entire json structure. But yes normally it would be the best way – Vollan Aug 19 '19 at 12:31
  • @Vollan I tried this way also but it says " Initializer for conditional binding must have Optional type, not '[String : Any]'", So mean it's also not working in my case. – Haroon khan Aug 19 '19 at 14:29