How to create JSON from a dictionary in Swift 4?

前端 未结 3 586
礼貌的吻别
礼貌的吻别 2021-01-12 23:45

Edit: I have read the other answers on SO for the same issue , however Im unable to get the desired output. I have tried many variations as suggested in other questions but

3条回答
  •  甜味超标
    2021-01-13 00:35

    Now JSONEncoder can do the work easily.

    let encoder = JSONEncoder()
    
    encoder.outputFormatting = .prettyPrinted
    
    let data = try encoder.encode(yourDictionary)
    
    print(String(data: data, encoding: .utf8)!)
    

提交回复
热议问题