Question: I have `struct` that conforms to protocol `Codable`. Some properties are decoded from JSON fields so I use the `CodingKeys` enum for that. But there are few properties that are not in my JSON and I need to calculate them from decoded JSON properties. For example, if you get a `Zip code` from JSON, I want to calculate `City` from it.

I don't want `City` to be an optional String. So I try to calculate it right after my `Zip code` field is decoded from JSON.

```swift
struct Place: Codable {
   var name: String
   var zipcode: String
   // ... Lot of other properties decoded from JSON

   var city: String // This property has to be calulated after `zip code` is decoded

   enum CodingKeys: String, CodingKey {
      case name = "placeName"
      case zipcode = "NPA"
      // other properties from JSON
   }
}

```

I've tried [this](https://stackoverflow.com/questions/48280631/swift-codable-init) solution to rewrite `init(from decoder: Decoder)`. But that means I need to manually write each property I need to decode. As I have a lot, I would prefer to let default init decoder does it job, and then add my code to calculate `City`.

Is there a way to do something like : call default init with decoder, then add some code ?

I was also thinking about computed property. But as calculating `City` from Zip code is quite lot of code, I don't want that it is always computed.

I need something like :

```swift
init(from decoder: Decoder) throws {
   // <- Call default init from decoder
   city = CityHelper.city(from: zipcode) // quite heavy code in there
}

```

Answer: 
I have found a solution but it needs to use `class` instead of `struct`.

I store decoded properties in a parent class. Then I extend this class and add calculated properties in it.

As it is not possible to extend `struct`, I had to use `class`. Maybe it is a first step to a better solution and it can help anyone find something better.

```swift
class DecodedPlace: Codable {
    var name: String
    var zipcode: String

    enum CodingKeys: String, CodingKey {
        case name = "placeName"
        case zipcode = "NPA"
    }
}

class Place: DecodedPlace {
    var city: String

    required init(from decoder: Decoder) throws {
        city = "" // Default value just to call super.init.

        try super.init(from: decoder)

        city = CityHelper.city(from: zipcode)
    }
}

```
You can use a computed property that relies on the zip to return a city. So you'd have to figure out the logic of how to map zip back to city with some method but here's a simple example I cooked up:

```
struct Rectangle : Codable{
        var width : Float
        var height : Float

        var area : Float{
            return width * height
        }
    }

```

Usage:

```
let myRect = Rectangle(width: 5.0, height: 5.0)
 print(myRect.area)

```

Result:
25.0