quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Feature: Add swift default init()

Open MicahKimel opened this issue 2 years ago • 1 comments

It would be super cool if for swift there could be a default init option where it would add a default init() to be able to convert the following

struct Sale: Codable {
    var code: Int 
    var desc: String
}

into something like this

struct Sale: Codable {
    var code: Int 
    var desc: String

    init(){
        self.code = 0
        self.desc = ""
    }

    init(code: Int, desc: String){
        self.code = code
        self.desc = desc
    }
}

MicahKimel avatar Apr 12 '23 14:04 MicahKimel

I would actually really like it if quicktype would read the "default" from the schema file. Example:

{
  "title": "Codable",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "code": {
      "type": "number"
      "default": 0 // <---------- HERE
    },
    "desc": {
      "type": "string"
      "default": "" // <---------- HERE
    },
  }
}

This way, we can control what those default values would be when they are not provided by the json data file.

mean-ui-thread avatar Sep 30 '23 20:09 mean-ui-thread