Home:ALL Converter>Casting an AnyObject to a Dictionary nested in an Array

Casting an AnyObject to a Dictionary nested in an Array

Ask Time:2016-10-08T14:03:02         Author:Ignatius_Gim

Json Formatter

I am looking to cast an AnyObject variable as a Dictionary nested in an Array. For example, I declare the variable in my function:

var items: [[String:String]] = [
     [
         "pid": "1",
         "content": "123",
         "vote": "1",
         "city": "New York",
         "country": "United States"
     ]
 ]

Then I fetch a JSON object from an HTTP request and convert it into an AnyObject. Then I am trying to append the data fetched by the HTTP request as an AnyObject onto the original variable.

This process fails at the casting of the AnyObject into the desired [[String: String]] form.

func updateTable(data: AnyObject?) {
    let data_array = (data as! NSArray) as! Array<Dictionary<String, String>>

    self.items += data_array
}

This function include the casting of the variable and the addition of the arrays.

How can I cast this variable correctly?

EDIT I forgot to mention that I cast my Serialized JSON Output as [AnyObject] before passing it through the function

Author:Ignatius_Gim,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39929224/casting-an-anyobject-to-a-dictionary-nested-in-an-array
yy