On lesson of the networking course

I cannot see the ratings of Yelp API

here’s the error:

No image named ‘regular_4.7’ found in asset catalog for /Users/kelvinricafort/Library/Developer/CoreSimulator/Devices/4AFF8550-9860-446F-848E-8412CD63F1DB/data/Containers/Bundle/Application/CC5AE31D-F22E-43AB-847D-BD6E9048675A/City Sights.app

You will need to round the rating to the nearest 0.5 since the images provided in the resources are in increments of 0.5.

If you were to modify your businessSearch function so that you could loop through the decoded data and for each rating use the ceil() method to round to the nearest 0.5. Example:

I’ll use the DataService code that Chris Ching posted to Dropbox - Lesson 10

struct DataService {
    
    let apiKey = Bundle.main.infoDictionary?["API_KEY"] as? String
    
    func businessSearch() async -> [Business] {
        
        // Check if api key exists
        guard apiKey != nil else {
            return [Business]()
        }
        
        // 1. Create url
        if let url = URL(string: "https://api.yelp.com/v3/businesses/search?latitude=35.665517&longitude=139.770398&categories=restaurants&limit=10") {
            
            // 2. Create request
            var request = URLRequest(url: url)
            request.addValue("Bearer \(apiKey!)", forHTTPHeaderField: "Authorization")
            request.addValue("application/json", forHTTPHeaderField: "accept")
            
            // 3. Send request
            do {
                let (data, response) = try await URLSession.shared.data(for: request)
                
                // 4. Parse the JSON
                let decoder = JSONDecoder()
                let result = try decoder.decode(BusinessSearch.self, from: data)
                
                return result.businesses
            }
            catch {
                print(error)
            }
        }
        
        return [Business]()
    }
}

Change that to:

struct DataService {
    
    let apiKey = Bundle.main.infoDictionary?["API_KEY"] as? String
    
    func businessSearch() async -> [Business] {
        
        // Check if api key exists
        guard apiKey != nil else {
            return [Business]()
        }
        
        // 1. Create url
        if let url = URL(string: "https://api.yelp.com/v3/businesses/search?latitude=35.665517&longitude=139.770398&categories=restaurants&limit=10") {
            
            // 2. Create request
            var request = URLRequest(url: url)
            request.addValue("Bearer \(apiKey!)", forHTTPHeaderField: "Authorization")
            request.addValue("application/json", forHTTPHeaderField: "accept")
            
            // 3. Send request
            do {
                let (data, response) = try await URLSession.shared.data(for: request)
                
                // 4. Parse the JSON
                let decoder = JSONDecoder()
                let result = try decoder.decode(BusinessSearch.self, from: data)

                var businesses = result.businesses

                for index in 0..<businesses.count {
                    if let rating = businesses[index].rating {
                        let newRating = ceil(rating / 0.5) * 0.5
                        businesses[index].rating = newRating
                    }
                }
                return businesses
            }
            catch {
                print(error)
            }
        }
        
        return [Business]()
    }
    
}

I experienced the same issue with the ratings returned by the API. i was looking for a solution to display correctly in the UI, but without changing the data. I found a rather elegant solution online. One way to round the rating is to double the number, round the result and then divide by two.

This could be implemented in-line, but for clarity I added it as a helper method. I call this from the string image name for the Image as:
“regular_(TextHelper.roundToClosestHalf(value: b.rating ?? 0))”