0

Here I create a Swift Dictionary.

  var paramDict: [String: String] = [
      "tour_type": "tour",
      "zone_id":   txtRegoin.tag.description,
      "district_from_id" : txtLocationFrom.tag.description,
      "district_to_id" : txtLocationTo.tag.description,
      "start_date" :  selectedStartTime.toString(dateFormat: BBTIMESTAMP_YYYYMMDD),
      "start_time" :  selectedStartTime.toString(dateFormat: BBTIMESTAMP_HH_MM),
    
      "return_date" :  selectedEndTime.toString(dateFormat: BBTIMESTAMP_YYYYMMDD),
      "return_time" :  selectedEndTime.toString(dateFormat: BBTIMESTAMP_HH_MM),
      
      "vehicle_type" : vehicleObjArr[selectedVehicleObj].brand!,
      "capacity" :  (vehicleObjArr[selectedVehicleObj].capacity?.description)!,

      
      "comment" : textDetails.text ?? "",
      "passengers" : numOfPassengers.description

  ]

If I debubg the dictionary.

po paramDict

 ▿ 14 elements
      ▿ 0 : 2 elements
        - key : "passengers[0]"
        - value : "27785"
      ▿ 1 : 2 elements
        - key : "zone_id"
        - value : "1"
      ▿ 2 : 2 elements
        - key : "return_time"
        - value : "13:20"
      ▿ 3 : 2 elements
        - key : "start_date"
        - value : "2020-07-01"
      ▿ 4 : 2 elements
        - key : "start_time"
        - value : "13:20"
      ▿ 5 : 2 elements
        - key : "capacity"
        - value : "7"
      ▿ 6 : 2 elements
        - key : "district_from_id"
        - value : "1"
      ▿ 7 : 2 elements
        - key : "return_date"
        - value : "2020-07-02"
      ▿ 8 : 2 elements
        - key : "tour_type"
        - value : "tour"
      ▿ 9 : 2 elements
        - key : "passengers"
        - value : "7"
      ▿ 10 : 2 elements
        - key : "district_to_id"
        - value : "3"
      ▿ 11 : 2 elements
        - key : "vehicle_type"
        - value : "Sedan"
      ▿ 12 : 2 elements
        - key : "comment"
        - value : "Ads adds"
      ▿ 13 : 2 elements
        - key : "passengers[1]"
        - value : "23132"

This is not well readable format. Especially if you send this dictionary to your backend developer.ie. I am facing a post problem and multiple times gave my dictionary which printed on debug mode. Its not good readable formatted output.

So, How it possible to good looking readable format?

Shourob Datta
  • 1,886
  • 22
  • 30
  • And what is a good format? The above output is readable so why would it be an issue to another developer? – Joakim Danielson Jun 30 '20 at 09:24
  • Does this answer your question? [Is there a way to pretty print Swift dictionaries to the console?](https://stackoverflow.com/questions/38773979/is-there-a-way-to-pretty-print-swift-dictionaries-to-the-console). You could also look at https://github.com/YusukeHosonuma/SwiftPrettyPrint – Andrew Jun 30 '20 at 09:44

2 Answers2

0

This is how you print a more readable format of dictionary. If you want to debug

(lldb) po print(paramDict as NSDictionary)
{
    capacity = 5;
    comment = Assad;
    "district_from_id" = 1;
    "district_to_id" = 1;
    "employees[0]" = 59903;
    passengers = 2;
    "return_date" = "2020-07-10";
    "return_time" = "14:34";
    "start_date" = "2020-07-08";
    "start_time" = "14:34";
    "tour_type" = tour;
    "vehicle_type" = Sedan;
    "zone_id" = 1;
}

Or

Normal Print

print(paramDict as NSDictionary)
Shourob Datta
  • 1,886
  • 22
  • 30
0

Simply use print(_:) in the console like so,

po print(paramDict)
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • `paramDict.description` will print in the `String` format including the escape characters. Example: `"[\"tour_type\": \"tour\"]"` – PGDev Jun 30 '20 at 09:35