How to access Algolia index and search properties?

I’m a iOS Swift dev using Swift 5 and I just updated my Algolia pod from pod 'AlgoliaSearch-Client-Swift', '~> 5.0' to
pod 'InstantSearchClient', '~> 7.0'.

I changed the headers from import AlgoliaSearch to import InstantSearchClient

Once I did that I no longer had access to the Algolia data type:

The docs says:

let index = client.index(withName: "contacts")
index.search(Query(query: "s"), completionHandler: { (content, error) -> Void in
  if error == nil {
      print("Result: \(content!)")
  }
})

No where on that page does it say what the datatype of client is so I took a wild guess and used: var index: Client! which worked. I’m a little perplexed as to why that page doesn’t say anything about Client being a datatype.

Anyway now that I have access to the Client I no longer have access to the index or search properties:

.

How do I access the index and search properties?

Here is the code:

// import AlgoliaSearch // this is from the old pod
import InstantSearchClient

var index: Client! // before changing the pod I used AlgoliaSearch.Index! which worked fine
let query = Query()
var searchId = 0
var displayedSearchId = -1
var loadedPage: UInt = 0
var nbPages: UInt = 0

override func viewDidLoad() {
    super.viewDidLoad()

    let apiClient = Client(appID: "12345", apiKey: "ABC...XYZ")

    // *** error ***
    index = apiClient.index(withName: "myIndex")

    query.hitsPerPage = 15
    query.attributesToRetrieve = ["flavor", "color"]
}

func updateSearchResults(for searchController: UISearchController) {
    
    query.query = searchController.searchBar.text
    let curSearchId = searchId
    
    // *** error ***
    index.search(query, completionHandler: { [weak self] (data, error) in
    })
}

func loadMore() {

    if loadedPage + 1 >= nbPages {
        return
    }
    let nextQuery = Query(copy: query)
    nextQuery.page = loadedPage + 1

    // *** error ***
    index.search(nextQuery, completionHandler: { [weak self] (data , error) in
    })
}

Hi @LanceSamaria,

In order for the snippet to work, just change

var index: Client! 

to:

var index: Index!

Cheers,
Guy

Hi Guy, thanks for the response. It didn’t work. It seems the SwiftyJSON library (a separate library I’m using) also has an Index type

When I followed what you said and changed the datatype from Client to Index I got this error:

07%20AM

I pressed the suggested fix (lower right hand corner of the first pic) and it added <Any> which is a generic type to it but now the error is:

12%20AM

It can’t tell the difference between the both of them or better yet which one to use

I got it to work. I had to add InstantSearchClient.Index to it:

var index: InstantSearchClient.Index!

1 Like