Poskytuje více detailů:
Snažím se získat přístup k úrovni z API (odkazované níže), který má v jeho struktuře, množství na začátku jedné z úrovní (25th_percentile). Jsem schopen vytvořit konstantní v mém modelu, protože nemůžu začít nechat hodnotu s číslem (tj, 25 v 25th_percentile). Jak můžete přistupovat k datům na 25. percentilu úrovni?
Můj kód je následující. Když jsem se použít enum, jak je navrženo, dostal jsem "ne podle dekódovatelný protokol" chyba.
//
// SchoolData.swift
//
//
import Foundation
struct Results: Decodable {
let results: [SelectedSchool]
}
struct SelectedSchool: Decodable, Identifiable {
let id: Int
let fed_sch_cd: String
let latest: LatestData
}
struct LatestData: Decodable {
let school: SchoolDetails
let admissions: AdmissionsDetails
}
struct SchoolDetails: Decodable {
let name: String
let school_url: String
}
struct AdmissionsDetails: Decodable {
let admission_rate: AdmissionRateDetails
let sat_scores: SATScores
}
struct SATScores: Decodable {
let midpoint: SATSubjects
let the25th_percentile: SATSubjects2
enum CodingKeys: String, CodingKey {
case the25th_percentile = "25th_percentile"
}
}
struct SATSubjects: Decodable {
let math: Int?
let writing: Int?
let critical_reading: Int?
}
struct SATSubjects2: Decodable {
let math: Int?
let writing: Int?
let critical_reading: Int?
}
struct AdmissionRateDetails: Decodable {
let overall: Float?
}
struct URLName {
let partialURL = "https://api.data.gov/ed/collegescorecard/v1/schools?school.name="
var searchTerm: String = ""
let apiKey = "&api_key=myapikey"
}