skip to Main Content

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

3

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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
        }
    }
    
    Login or Signup to reply.
  3. 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"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search