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
Name of the
Type
is a reserved word in swift thats why you are gettingError : 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 nameType
, you must use it likeYou can name properties whatever you like, just add coding keys, so decoder knows where to find it.
You can rename Type as responseType and add enum CodingKeys for decoding.