arraysswiftswift-dictionary

Swift : How do i create an array of dictionaries, where each dictionary contains an array inside them?


I am trying to load some data from my plist into an array. While declaring an array i did something like this

var natureList : [Dictionary] = [] as! [Dictionary]

I am getting this error enter image description here

Here is my plist file enter image description here

So how do i declare an array to load this data from my plist. ? I know its simple but this small thing is eating my head. Any help appreciated.


Solution

  • It should be declared as follows:

    var natureList = [[String: Any]]()
    

    or as @LeoDabus advised (thanks to him):

    var natureList: [[String: Any]] = []
    

    Means that natureList is an array of dictionaries of strings as keys and any as values.

    If you are aiming to declare it using Dictionary -which is uanessacry-, you could also do it like this:

    var natureList = [Dictionary<String, Any>]()