Search API Response - hits array is NSSingleObjectArrayI

Howdy,

I was hoping to get help figuring out how to handle the response from a search:

index.search(Query(query: searchBar.text), completionHandler: { (content, error) -> Void in
                        if error == nil {
                            print(content)
                        }
                    })

            let query = Query(query: searchBar.text)

            index.search(query, completionHandler: { (content, error) -> Void in
              if error == nil {
                 // print("Result: \(content!)")
              }
            })

In my case, “content” is as shown below. Is there a reason why I am getting NSSingleObject array for the “hits” array and not the nicely formatted JSON response described here on Alglia’s API Response page? I am a bit new to this so i apologize if i’m making some kind of simple beginner mistake!

XCode 11.3
pod ‘InstantSearchClient’, ‘~> 7.0’

   Optional(["page": 0, 
"exhaustiveNbHits": 1, 
"params": query=Laptop, 
"hits": <__NSSingleObjectArrayI 0x28241cb10>(
    {
    "_highlightResult" =     {
        header =         {
            fullyHighlighted = 1;
            matchLevel = full;
            matchedWords =             (
                laptop
            );
            value = "<em>laptop</em>";
        };
        itemDetail =         {
            matchLevel = none;
            matchedWords =             (
            );
            value = "MacBook Air, Grey 2018, great condition. bike";
        };
    };
    cont = 1;
    date = "1586047833.580743";
    header = laptop;
    itemDetail = "MacBook Air, Grey 2018, great condition. bike";
    mode = fixed;
    objectID = Q21dMGcvRcgIvSVCqs8U;
    price = 890;

    }
    ) , 
   "query": Laptop, 
    "hitsPerPage": 20, 
    "nbHits": 1, 
    "nbPages": 1, 
    "processingTimeMS": 1])

Hi Nick,

This is an expected behaviour. We use JSONSerialization which is a default Foundation component and it decides internally, how to represent each part of JSON as a Foundation object. It could be an NSArray object but due to some optimization it’s an NSSingleObjectArray. You can iterate normally through it.

Hope it helps!

Awesome, thank you for the quick reply!

You are totally right - this seemed to work:

 index.search(Query(query: searchBar.text), completionHandler: { (content, error) -> Void in
                                                  if error == nil {
                                                   // print(content!)
                                                      do{
                                                        let searchResults = try JSONSerialization.data(withJSONObject: content!, options: .prettyPrinted)
                                                          let searchData = try! JSONDecoder().decode(SearchData.self, from: searchResults)
                                                         print(searchData.hits[0].itemDetail)
                                                         print(searchData.nbHits)
                                                          } catch {
                                
                                                          print("error - keep going")
                                                      }
                        
                                                  }
                                              })

where SearchData is defined as:

struct SearchData: Codable {

let hitsPerPage: Int

let nbHits: Int

let nbPages: Int

let hits: [searchHits]

}

struct searchHits: Codable {

let cont : Bool

let date : Double

let header : String

let itemDetail : String

let objectID : String

let price : Int

}

All that prints:

MacBook Air, Grey 2018, great condition. bike
1

Thank you!