Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 689 Bytes

README.md

File metadata and controls

29 lines (22 loc) · 689 Bytes

CodableUtils

A set of helper functions of any Codable object.

Features:

  • Get class name from a Codable object:
let nameString = MyModel.className
print(nameString) // output: MyModel
  • Convert a Codable object into a dictionary:
let obj = MyModel(name: "John", age: 24)
let objDict = obj.toDictionary()

// Optionally use a custom JSONEncoder
let objDict = obj.toDictionary(encoder: CustomJSONEncoder())
  • Convert a dictionary into a Codable object:
let objDict = ["name": "John", age: 24]
let obj = objDict.toModel(MyModel.self)

//Optionally use a custom JSONDecoder
let obj = objDict.toModel(MyModel.self, decoder: CustomJSONDecoder())