Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Store array custom array information

I have a doubt when it comes to store array information, this is my API

WalletService.shared.getSiteDetails { siteDetails, error in
                
            }

siteDetails is going to give me the decodeJson information,
one of multiple information that is retrieving is

public let fuelGrades: [MBFuelGrades]?

Which contains this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public struct MBFuelGrades: Codable {
    public let fuelType: String?
    public let price: Double?
    public let priceTier: String?
}

so when my API call response I get this array by printing
it could contain multiple elements not only one

po siteDetails?.fuelGrades
▿ Optional<Array<MBFuelGrades>>
  ▿ some : 1 element
    ▿ 0 : MBFuelGrades
      ▿ fuelType : Optional<String>
        - some : "REGULAR"
      ▿ price : Optional<Double>
        - some : 1.339
      ▿ priceTier : Optional<String>
        - some : "CREDIT"

what I trying to do is store the fuelType and price, in separate arrays

for example this:

var fuelGradesTypes: [String] = [""]
var fuelGradesPrice: [Double] = [0.0]

my question is how can I store this elements inside the arrays?

>Solution :

You need

guard let grades = siteDetails.fuelGrades else { return }

fuelGradesTypes = grades.compactMap { $0.fuelType }
fuelGradesPrice = grades.compactMap { $0.price }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading