15 C
London
Wednesday, September 4, 2024

ios – The way to use UUID as property title for Swift API information mannequin


I am at the moment utilizing an API to get information for a iOS app utilizing Swift.

I am having a problem creating the info mannequin as a result of one of many property makes use of a UUID as its title.
Does anybody know an answer for this ?

Right here is the API information in query :
The half I am unable to get in a mannequin is the half utilizing the title “11a4a746-30bb-4dfd-980f-39c4dfd84412”.

{
    "profiles": {
        "11a4a746-30bb-4dfd-980f-39c4dfd84412": {
            "profile_id": "11a4a746-30bb-4dfd-980f-39c4dfd84412",
             ...
        }

...

}

Right here is the info fashions I at the moment have :

struct Profiles: Codable {
    let profiles: ProfilesClass
}

struct ProfilesClass: Codable {
    let profile_id: The11A4A74630Bb4DFD980F39C4Dfd84412

    enum CodingKeys: String, CodingKey {
        case profile_id = "11a4a746-30bb-4dfd-980f-39c4dfd84412"
    }
}

enum SBError: Error {
    case invalidURL
    case invalidResponse
    case invalidData
    case dataCorrupted
    case keyNotFound
    case valueNotFound
    case typeMismatch
}

Right here is the info fetching perform

func fetchData() async throws -> Profiles {
        let endpoint = ENDPOINT URL GOES HERE IN ACTUAL CODE
        
        guard let url = URL(string: endpoint) else {
            throw SBError.invalidURL
        }
        
        let (information,response) = attempt await URLSession.shared.information(from: url)
        
        guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
            throw SBError.invalidResponse
        }
        
        do {
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            return attempt decoder.decode(Profiles.self, from: information)
        } catch let DecodingError.dataCorrupted(context) {
            print(context)
            throw SBError.dataCorrupted
        } catch let DecodingError.keyNotFound(key, context) {
            print("Key '(key)' not discovered:", context.debugDescription)
            print("codingPath:", context.codingPath)
            throw SBError.keyNotFound
        } catch let DecodingError.valueNotFound(worth, context) {
            print("Worth '(worth)' not discovered:", context.debugDescription)
            print("codingPath:", context.codingPath)
            throw SBError.valueNotFound
        } catch let DecodingError.typeMismatch(kind, context)  {
            print("Kind '(kind)' mismatch:", context.debugDescription)
            print("codingPath:", context.codingPath)
            throw SBError.typeMismatch
        } catch {
            print("error: ", error)
            throw SBError.invalidData
        }
    }

I attempted utilizing

enum CodingKeys: String, CodingKey {
        case profile_id = "11a4a746-30bb-4dfd-980f-39c4dfd84412"
    }

however this does not appear to work. I nonetheless get a KeyNotFound error.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here