3

Convert [String:Any] dictionary array to [String:String] in Swift

I have an array of dictionary <String,Any> type. Now I want to convert to string because I want to show data in textField and text field not understand Any data type.

My data like this:

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

[
    [
        "Area" : "",
        "Good" : "-",
        "Level" : 2,
        "Link" : "<null>",
        "Photo" : "-",
        "Repair" : "-",
        "Section" : "Others"
    ],

    [
        "Area" : "",
        "Good" : "N",
        "Level" : 2,
        "Link" : "http://google.com",
        "Photo" : 1,
        "Repair" : "Y",
        "Section" : "Grounds"
    ]

]

and I want new Array Dictionary:

var myarr = [[String:String]]()
5
  • You want to convert [[String:Any]] to [[String:String]] ? Commented May 2, 2017 at 5:06
  • yes i wan't to convert String String as?String is not work Commented May 2, 2017 at 5:07
  • What do you want ["test4": true] to convert to? Commented May 2, 2017 at 5:13
  • @MikeTaverne check my updated question Commented May 2, 2017 at 6:09
  • This helped me. Commented May 22, 2017 at 13:00

3 Answers 3

7

Map is your friend, maybe something like

let stringDictionaries: [[String: String]] = myarr.map { dictionary in
    var dict: [String: String] = [:]
    dictionary.forEach { (key, value) in dict[key] = "\(value)" }
    return dict
}
Sign up to request clarification or add additional context in comments.

13 Comments

thank you it's work properly and not get any error in type converting
This answer is wrong. It adds an extra layer of arrays to the results.
please give another solution @rmaddy i am waiting for your solution
The update is wrong too. flatmap doesn't change anything here. Please verify your answer before posting. A simple test in the playground should be done.
Your test isn't valid. Test with a dictionary that has more than one key/value pair just like the data in the question.
|
5

Here is one solution that actually gives the correct results:

let myarr = [
    [
        "Area" : "",
        "Good" : "-",
        "Level" : 2,
        "Link" : "<null>",
        "Photo" : "-",
        "Repair" : "-",
        "Section" : "Others"
    ],

    [
        "Area" : "",
        "Good" : "N",
        "Level" : 2,
        "Link" : "http://someurl",
        "Photo" : 1,
        "Repair" : "Y",
        "Section" : "Grounds"
    ]
]


var newarr = [[String:String]]()
for dict in myarr {
    var newdict = [String:String]()
    for (key, value) in dict {
        newdict[key] = "\(value)"
    }
    newarr.append(newdict)
}
print(newarr)

Output:

[["Level": "2", "Area": "", "Good": "-", "Link": "<null>", "Repair": "-", "Photo": "-", "Section": "Others"], ["Level": "2", "Area": "", "Good": "N", "Link": "http://someurl", "Repair": "Y", "Photo": "1", "Section": "Grounds"]]

Comments

1

You can create new dictionary using following code:

var newArray:[[String: String]] = []
for data in myarr {
    var dict: [String: String] = [:]
    for (key, value) in data {
        let strData = String(describing: value)
        dict[key] = strData
    }
    newArray.append(dict)
}

2 Comments

This code produces the wrong result. It only works if each dictionary has one key/value pair.
I've edited my code. So now you can check the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.