Tuesday, June 30, 2026
HomeiOS Developmentios - How do I convert to in Xcode?

ios – How do I convert [String:Any] to [String:String] in Xcode?

[ad_1]

A dictionary of kind [String: Any] can include key/worth pairs of kind [String: String] in addition to pairs the place. the worth will be any kind.

You would use compactMap() to transform the subset of key/worth pairs in your dictionary which are of kind [String: String] to a dictionary of kind [String: String] with code like this:

let aDict: [String: Any] = ["key1": 1, "key2": "two", "key3": 3.1415, "key4": "four", "key5": "five"]

let dictWithStrings = aDict.compactMap { (merchandise) -> [String: String]? in
    guard let stringValue = merchandise.worth as? String else { return nil }
    return [item.key: stringValue]
    }

Alternately, you can map each entry in your dictionary to [String:String] utilizing String(describing:):

let dictWithStrings = aDict.map { (merchandise) -> [String: String] in
    return [item.key: String(describing: item.value)]
    }

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments