The Restaurant Detail screen's overall rating label displays 0.0, and the ratings view displays 3.5 stars, regardless of the actual rating. To add an overall rating, you need to get the ratings from all the reviews and average them. Let's add a new method to CoreDataManager to do this. Do the following steps:
- Click CoreDataManager.swift inside the Project navigator (inside the Core Data folder in the Misc folder), and add the following method before the addReview(_:) method:
func fetchRestaurantRating(by identifier:Int) -> Float {
let reviews = fetchReviews(by: identifier).map({ $0 })
let sum = reviews.reduce(0, {$0 + ($1.rating ?? 0)})
return sum / Float(reviews.count)
}
In this method, all reviews for a particular restaurant are fetched from Core Data. The reduce() method takes a closure, which is used to...