Swift Decoding JSON - Unresolved Identifier 'content'

I’m having an issue with the JSON in a Swift project.

3 errors in the following code…
Use of unresolved identifier ‘content’ (x2)
Use of unresolved identifier nbsp

import UIKit
import SDWebImage
import AlgoliaSearch
import AFNetworking
import SwiftyJSON

var sharks = [WhaleShark]()

var sharkIndex: AlgoliaSearch.Index!
let query = Query()
var searchId = 0
var displayedSearchId = -1
var loadedPage: UInt = 0
var nbPages: UInt = 0

func updateSearchResults(for searchController: UISearchController) {
    query.query = searchController.searchBar.text
    let curSearchId = searchId
    sharkIndex.search(query, completionHandler: { (data, error) -> Void in
        if (curSearchId <= self.displayedSearchId) || (error != nil) {
            return // Newest query already displayed or error
        }
        self.displayedSearchId = curSearchId
        self.loadedPage = 0 // Reset loaded page
        // Decode JSON
        **guard let hits = content!["hits"] as? [[String: AnyObject]] else { return }** ERROR
        **guard let nbPages = content!["nbPages"] as? UInt else { return }** ERROR
        self.nbPages = nbPages
        **&nbsp;var tmp = [WhaleShark]()** ERROR
        for hit in hits {
            tmp.append(WhaleShark(json: hit))
        }
        // Reload view with the new data
        self.sharks = tmp
        self.tableView.reloadData()
    })
    self.searchId += 1
}

Also having errors in the loadMore function

Cannot convert value type ‘JSON’ to expected argument type ‘[String:AnyObject]’
Value of type ‘[WhaleShark]’ has no member ‘extend’

func loadMore() {
    if loadedPage + 1 >= nbPages {
        return // All pages already loaded
    }
    let nextQuery = Query(copy: query)
    nextQuery.page = loadedPage + 1
    sharkIndex.search(nextQuery, completionHandler: { (data , error) -> Void in
        if (nextQuery.query != self.query.query) || (error != nil) {
            return // Query has changed
        }
        self.loadedPage = nextQuery.page!
        let json = JSON(data!)
        let hits: [JSON] = json["hits"].arrayValue
        var tmp = [WhaleShark]()
        for record in hits {
            **tmp.append(WhaleShark(json: record))** ERROR
        }
        // Display the new loaded page
        **self.sharks.extend(tmp)** ERROR
        self.tableView.reloadData()
    })
}

I’m using Swift 3.1 and Xcode 8.3.2
All the code is from the swift instant search tutorial
https://www.algolia.com/doc/guides/search/instant-search/swift/

I installed the InstantSearch pod and went along the lines of the example project and now am getting results…

1 Like

@phil.manning11 glad to hear you got it working!

The reason why you were getting errors before such as ‘nbsp’ is because of copy pasting the code. If you check the line above, nbsp doesn’t mean anything in swift and should be removed from the snippet of code. Thanks for reporting that!