0

After reading many sources unable to get it there are two issues one : nested data, second name of one property is "Type" which is not accepted by swift saying conflict with .... can someone sort this out or explain straight proper way here is the json response

"IsSuccess": true,
"Message": "Data Returned",
"ResponseData": [
    {
        "PackageId": 1025,
        "PackageName": "Progesterone",
        "Price": 00.0,
        "DiscountedPrice": 1.0,
        "Type": "Test",
        "TestPackageGroupId": 3,
        "SampleTypeList": [
            {
                "TestSampleTypeId": "50",
                "SampleName": "Serum",
                "ColourCode": "#FFB500"
            }
        ]
    },
    {
        "PackageId": 1916,
        "PackageName": "24 hour Albumin creatinine ratio (ACR)",
        "Price": 00.0,
        "DiscountedPrice": 1.0,
        "Type": "Test",
        "TestPackageGroupId": 3,
        "SampleTypeList": [
            {
                "TestSampleTypeId": "66",
                "SampleName": "24 hrs Urine",
                "ColourCode": "#212DC1"
            }
        ]
    },

how to write data class for above the property "Type" is unaccepted if I use it as it is

   struct PriceListAll : Codable
        {
          let Message: String
          let IsSuccess: Bool
          let ResponseData: [ResponseData]
         }


     struct ResponseData:Codable
        {

                let PackageId: Int
                let PackageName: String
                let Price: Double
                let DiscountedPrice: Double
                let Type: String
                let TestPackageGroupId: Int
                let SampleTypeList: [SampleTypeList]
             }


      struct SampleTypeList:Codable
          {
             let TestSampleTypeId: Int
             let SampleName: String
             let ColourCode: String

         }

Error : Type member must not be named 'Type', since it would conflict with the 'foo.Type' expression

tintin
  • 335
  • 2
  • 8

3 Answers3

0

Name of the Type is a reserved word in swift thats why you are getting Error : Type member must not be named 'Type', since it would conflict with the 'foo.Type' expression this error message . If your model represent a key with name Type , you must use it like

let `Type`: String
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
0

You can name properties whatever you like, just add coding keys, so decoder knows where to find it.

struct ResponseData: Codable {
    let PackageId: Int
    let PackageName: String
    let Price: Double
    let DiscountedPrice: Double
    let MyCustomTypeName: String
    let TestPackageGroupId: Int
    let SampleTypeList: [SampleTypeList]

    enum CodingKeys: String, CodingKey {
        case PackageId
        case PackageName
        case Price
        case DiscountedPrice
        case MyCustomTypeName = "Type"
        case TestPackageGroupId
        case SampleTypeList
    }
}
Konstantin
  • 197
  • 3
  • 12
0

You can rename Type as responseType and add enum CodingKeys for decoding.

struct ResponseData: Codable {

    let responseType: String

    enum CodingKeys: String, CodingKey {
        case responseType = "Type"
    }
}
stoikokolev
  • 484
  • 5
  • 9
  • Type 'ResponseData' does not conform to protocol 'Decodable' is the error when using enum Codingkeys – tintin Mar 22 '22 at 07:44