Using Codable in AlgoliaSearchClient to model results

Hi,

I’m trying to use the Algolia Swift API to retrieve and map results to a Codable struct, but I’ve got very confused as to how to proceed beyond the point below. Can anyone advise please?

    func search(_ forText:String){
    
    let client = SearchClient(appID: ApplicationID(stringLiteral: K.Secure.algoliaApplicationID), apiKey: APIKey(stringLiteral: K.Secure.algoliaAPIKey))
    let index = client.index(withName: IndexName(stringLiteral: K.Secure.algoliaIndex))

    let query = Query(stringLiteral: forText)
    
    try? index.search(query: query){serverResponse in
        switch serverResponse{
            case .failure(let error):
                print(error.localizedDescription)
            case .success(let result):
                let hits:Array = result.hits

        // -- what here to map to the Hit object defined below?

        }
    }
}

struct Hit: Codable {
    let objectID:String
    let title:String?
    let body:String?
}

Hi @jp12,

The hits property you mentioned provides an array of JSON objects.
If you want to cast the hits to your custom Codable model, use extractHist method as follows:

case .success(let result):
  let hits: [Hit]? = try? result.extractHits()