-1

I am trying to create a dictionary in a static way and obtain the data, but I am not correct. I mean because it is only an array of string and any, but in the image it has brackets and braces. Any help I will appreciate a lot, thanks for your time

let responseDevice : [String : Any] = [
         "date_s" : "2021-02-18",
         "id_c" : "4",
         "id_d" : 1,
         "data" : [
             "Peso" : 34,
             "Fc" : -1,
             "Age" : 34,
            "Name" : "July"
             ],
        "flags" : 0,
        "error" : 0
         
     ]
    if  let date_s =  responseDevice["date_s"] as?  String,
        let dat = responseDevice["data"] as?  [String : Any],
        let peso =  dat["Peso"] as?  Int {
        print(date_s)
        print(peso)
    }
    
    print("log :\(responseDevice)")

result:

2021-02-18
34
log :["id_c": "4", "error": 0, "id_d": 1, "flags": 0, "date_s": "2021-02-18", "data": ["Peso": 34, "Fc": -1, "Age": 34, "Name": "July"]]

enter image description here

timbre timbre
  • 12,648
  • 10
  • 46
  • 77

1 Answers1

0

What you created is a Swift Dictionary. What you have on that image is JSON Object. It's not very clear what your goal is, so couple of basic pointers:

  • If you want to parse JSON into Dictionary, check this answer
  • If you simply want to include some JSON sample in your code (e.g. for testing), you can put it in triple quotes:
var myJSON = """
[paste your JSON here]
"""
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
  • Thank you for taking the time, I have that model and I have a type of function that gives me that dictionary as a result: func dataWasRead (data: [String: Any]) {...} This function returns that dictionary to me and what I want is to simulate or create a mock of that type and be able to obtain its values ​​and then store it in a struct or class, it does not matter, I find myself in that dilemma – José Leoncio Quispe Rodriguez Aug 11 '21 at 21:58