Thursday, June 27, 2024
HomeiOS DevelopmentIncomplete response getting parsed in swift iOS utilizing codable

Incomplete response getting parsed in swift iOS utilizing codable


I’m first time growing app for CarPlay iOS.
The place I’m fetching record of stations to be performed on CarPlay radio.

Following is the JSON response….

 {
  "stations": [
    {
      "id": "ce2a2901-dae3-48a1-a16c-5904287694d7",
      "market": "genre",
      "name": "Coles Radio",
      "tag": "colesradio",
      "description": "",
      "tagline": "",
      "isExclusive": false,
      "isDefault": true,
      "startDate": "2023-03-16T07:15:00.000Z",
      "endDate": "2099-12-31T00:00:00.000Z",
      "createdAt": "2023-03-16T17:15:00.000Z",
      "updatedAt": null,
      "links": null,
      "slugs": [
        "colesradio"
      ],
      "places": null,
      "transmissions": [
        {
          "id": "7af53831-5730-441c-861d-aea3e6c25290",
          "type": "ip",
          "streams": [
            {
              "id": "4a380cf9-3b5a-4a1c-8dfe-f39377969c64",
              "type": "mp3",
              "mimeType": "audio/mpeg",
              "url": "https://playerservices.streamtheworld.com/api/livestream-redirect/COLES_CONSUMER.mp3",
              "tritonMount": "COLES_CONSUMER",
              "bitrate": 96,
              "stationId": "ce2a2901-dae3-48a1-a16c-5904287694d7"
            },
            {
              "id": "5c2693a9-8c99-4182-8991-422c7b8d5e26",
              "type": "aac",
              "mimeType": "audio/aac",
              "url": "https://playerservices.streamtheworld.com/api/livestream-redirect/COLES_CONSUMERAAC48.m3u8",
              "tritonMount": "COLES_CONSUMERAAC48",
              "bitrate": 48,
              "stationId": "ce2a2901-dae3-48a1-a16c-5904287694d7"
            }
          ]
        }
      ],
      "branding": {
        "primaryColour": "#EB1C24",
        "secondaryColour": "#EB1C24",
        "tertiaryColour": null
      },
      "brandTag": "companions",
      "belongings": {
        "logos": {
          "app": {
            "panorama": [
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/app/landscape/[email protected]",
              "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/logos/app/panorama/[email protected]",
              "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/logos/app/panorama/[email protected]"
            ],
            "portrait": [
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/app/portrait/[email protected]",
              "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/logos/app/portrait/[email protected]",
              "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/logos/app/portrait/[email protected]"
            ]
          },
          "carPlay": [
            "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/carPlay/image.png"
          ],
          "chromeCast": [
            "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/chromeCast/image.png"
          ],
          "net": {
            "panorama": [
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/landscape/colour1.svg",
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/landscape/colour2.svg"
            ],
            "portrait": [
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/portrait/colour1.svg",
              "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/portrait/colour2.svg"
            ],
            "trimmed": {
              "panorama": [
                "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/trimmed/landscape/colour1.svg",
                "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/trimmed/landscape/colour2.svg"
              ],
              "portrait": [
                "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/trimmed/portrait/colour1.svg",
                "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/logos/web/trimmed/portrait/colour2.svg"
              ]
            }
          }
        },
        "artworks": [
          "https://hub-novaent-io-assets.s3.amazonaws.com/partners/colesradio/artworks/[email protected]",
          "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/artworks/[email protected]",
          "https://hub-novaent-io-assets.s3.amazonaws.com/companions/colesradio/artworks/[email protected]"
        ]
      }
    }
  ],
  "pagination": {
    "totalResults": 21,
    "fromResult": 21,
    "toResult": 21,
    "perPage": 1,
    "currentPage": 2,
    "lastPage": 2
  }
}

I’m utilizing Codable protocol to Parse the information.

Following is my protocol.

struct RadioStationResponseModel: Codable {
    let stations: [Station]?
    let pagination: Pagination
}

// MARK: - Pagination
struct Pagination: Codable {
    let totalResults, fromResult, toResult, perPage: Int?
    let currentPage, lastPage: Int?
}

// MARK: - Station
struct Station: Codable {
    let id: String?
    let market: Market?
    let identify, tag, description, tagline: String?
    let isExclusive, isDefault: Bool?
    let startDate: String?
    let endDate: EndDate?
    let createdAt: String?
    let updatedAt: JSONNull?
    let hyperlinks: Hyperlinks?
    let slugs: [String]?
    let places: [Location]?
    let transmissions: [Transmission]?
    let branding: Branding?
    let brandTag: String?
    let belongings: Belongings?
}

// MARK: - Belongings
struct Belongings: Codable {
    let logos: Logos?
    let artworks: [String]?
}

// MARK: - Logos
struct Logos: Codable {
    let app: App?
    let carPlay, chromeCast: [String]?
    let net: Net?
}

// MARK: - App
struct App: Codable {
    let panorama, portrait: [String]?
}

// MARK: - Net
struct Net: Codable {
    let padded: Padded?
    let trimmed: App?
    let panorama, portrait: [String]?
}

// MARK: - Padded
struct Padded: Codable {
    let panorama: [String]?
}

// MARK: - Branding
struct Branding: Codable {
    let primaryColour, secondaryColour: AryColour?
    let tertiaryColour: JSONNull?
}

enum AryColour: String, Codable {
    case cf202E = "#CF202E"
    case eb1C24 = "#EB1C24"
    case ee7723 = "#EE7723"
    case the000000 = "#000000"
    case the005094 = "#005094"
}

enum EndDate: String, Codable {
    case the20991231T000000000Z = "2099-12-31T00:00:00.000Z"
}

// MARK: - Hyperlinks
struct Hyperlinks: Codable {
    let facebookURL, instagramURL: String?

    enum CodingKeys: String, CodingKey {
        case facebookURL = "facebookUrl"
        case instagramURL = "instagramUrl"
    }
}

// MARK: - Location
struct Location: Codable {
    let metropolis, state, postcode, timezone: String?
}

enum Market: String, Codable {
    case style = "style"
    case metro = "metro"
}

// MARK: - Transmission
struct Transmission: Codable {
    let id: String?
    let sort: TransmissionType?
    let streams: [Stream]?
    let band: Band?
    let frequency: String?
}

enum Band: String, Codable {
    case am = "am"
    case dab = "dab"
    case fm = "fm"
}

// MARK: - Stream
struct Stream: Codable {
    let id: String?
    let sort: StreamType?
    let mimeType: MIMEType?
    let url: String?
    let tritonMount: String?
    let bitrate: Int?
    let stationID: String?

    enum CodingKeys: String, CodingKey {
        case id, sort, mimeType, url, tritonMount, bitrate
        case stationID = "stationId"
    }
}

enum MIMEType: String, Codable {
    case audioAAC = "audio/aac"
    case audioMPEG = "audio/mpeg"
}

enum StreamType: String, Codable {
    case aac = "aac"
    case mp3 = "mp3"
}

enum TransmissionType: String, Codable {
    case broadcast = "broadcast"
    case ip = "ip"
}

// MARK: - Encode/decode helpers

class JSONNull: Codable, Hashable {

    public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
            return true
    }

    public var hashValue: Int {
            return 0
    }

    public init() {}

    public required init(from decoder: Decoder) throws {
            let container = attempt decoder.singleValueContainer()
            if !container.decodeNil() {
                    throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unsuitable sort for JSONNull"))
            }
    }

    public func encode(to encoder: Encoder) throws {
            var container = encoder.singleValueContainer()
            attempt container.encodeNil()
    }
}

That is the best way I’m calling my API and dealing with response.

let netwokService : NetworkService = DefaultNetworkService()
    let request = RadioModel()
    netwokService.request(request, completion: { [weak self] lead to
      swap consequence{
      case .success(let radioStationResponse):
        print("Radio station : (radioStationResponse)")
      case .failure(let errorValue):
        print("Erro : (errorValue)")
      }
      
    })



func decode(_ information: Knowledge) throws -> RadioStationResponseModel {
    let decoder = JSONDecoder()
    let response = attempt decoder.decode(RadioStationResponseModel.self, from: information)
    print("Response acquired :- (response)")
    return response
  }

However the issue right here I’m dealing with is, I’m not getting second object from response.

enter image description here

I’m able to get solely first object. Pagination object I get as nil, however is has worth in it which I would like for my additional work.

Can anybody please assist me to grasp what’s incorrect I’m doing.

Thanks for assist upfront.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments