In Swift, how can I use an alias name for the struct/class which is mapped to object returned in JSON (from WebAPI call)?

31 Views Asked by At

I am making a call to a WebAPI which returns the following JSON. The JSON contains an object named group which I want to map in my Swift code using Codable protocol.

"group":{"ID":4,"OwnerId":22,"MemberId":17,"Name":"FirstGroup",
"Created":"2024-02-23","Active":1}

In Swift I have created the small struct so the mapping will be done.

struct Group: Codable{
   var id: Int64
   var OwnerId: Int64
.  
. // reset of items mapped
}

However, when I go to build, there is a clash between the struct Group I defined and the SwiftUI Group.

I believe I'm stuck now however, because when the mapping is done when I retrieve the JSON I know it will look for object named Group in my JSON to do the automatic mapping.

Is there a way to use an alias name for the Swift side but still map to the JSON's group object?

In this instance, I can't change the WebAPI to return another name.

0

There are 0 best solutions below