StoreSearch: Error in code on page 97

This is the code from the book on page 97:

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
   if !searchBar.text!.isEmpty {
    searchBar.resignFirstResponder()
  
    isLoading = true
    tableView.reloadData()
  
    searchResults = []
    hasSearched = true
  
    let queue = DispatchQueue.global()
  
    queue.async {
      let url = self.iTunesUrl(searchText: searchBar.text!)
    
      if let jsonString = self.performStoreRequest(with: url) {
        let jsonDictionary = self.parse(json: jsonString) {
        self.searchResults = self.parse(dictionary: jsonDictionary)
        self.searchResults.sort(by: <)
        print("DONE!")
        return
      }
        print("Error!")
     }
   }
 }

If you try to use the code above it will give an error as it is missing the ‘if’ keyword on the second let jsonDictionary = self....

Here is the correct code:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  if !searchBar.text!.isEmpty {
    searchBar.resignFirstResponder()
  
    isLoading = true
    tableView.reloadData()
  
    searchResults = []
    hasSearched = true
  
    let queue = DispatchQueue.global()
  
    queue.async {
       let url = self.iTunesUrl(searchText: searchBar.text!)
    
       if let jsonString = self.performStoreRequest(with: url) {
         if let jsonDictionary = self.parse(json: jsonString) {
           self.searchResults = self.parse(dictionary: jsonDictionary)
           self.searchResults.sort(by: <)
           print("DONE!")
           return
         }
         print("Error!")
       }
    } 
}

Hope that helps

What was intended is this:

so instead of the { there should be a , comma.

1 Like