1

I have a String

example:

"['name': 'consumer_nam', 'date': 'date']"

I want to convert this to a dictionary type in Swift4.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
李珈瑜
  • 11
  • 1
  • Welcome to StackOverflow. Try to be more specific, provide some code. What is your expected output? What is your current output? What are possible inputs? What have you tried that doesn't work? – Gustavo Vollbrecht Feb 11 '19 at 23:19
  • 2
    Possible duplicate of [Swift: How to convert string to dictionary](https://stackoverflow.com/questions/29221586/swift-how-to-convert-string-to-dictionary) – Pooja Kamath Feb 12 '19 at 02:30

2 Answers2

0

Dictionaries in Swift can be represented by [String : Any], and many other ways.

let dictionary : [String : Any] = ["name" : "consumer_nam", "date" : "date"]

If what you're trying to do is parse the String, you can take a look here and here.

Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
0

It looks like your String is supposed to be interpreted as a Dictionary. It looks a bit like a textual representation of JSON.

Find a spec for the exact format of that String, and then you write code to parse it.

It may be easier to convince whoever produced that string to produce for example a JSON document instead (which would be stored as Data, not String).

gnasher729
  • 51,477
  • 5
  • 75
  • 98