Book Image

iOS 12 Programming for Beginners - Third Edition

By : Craig Clayton
Book Image

iOS 12 Programming for Beginners - Third Edition

By: Craig Clayton

Overview of this book

Want to build iOS 12 applications from scratch with the latest Swift 4.2 language and Xcode 10 by your side? Forget sifting through tutorials and blog posts; this book is a direct route to iOS development, taking you through the basics and showing you how to put principles into practice. Take advantage of this developer-friendly guide and start building applications that may just take the App Store by storm! If you’re already an experienced programmer, you can jump right in and learn the latest iOS 12 features. For beginners, this book starts by introducing you to iOS development as you learn Xcode and Swift. You'll also study advanced iOS design topics, such as gestures and animations, to give your app the edge. You’ll explore the latest Swift 4.2 and iOS 12 developments by incorporating new features, such as the latest in notifications, custom-UI notifications, maps, and the recent additions in Sirikit. The book will guide you in using TestFlight to quickly get to grips with everything you need to get your project on the App Store. By the end of this book, you'll be ready to start building your own cool iOS applications confidently.
Table of Contents (27 chapters)
Free Chapter
1
Getting Familiar with Xcode

Adding an overall rating

To add an overall rating, we need to pull all of the reviews from Core Data, add them all together, and get an average. Let's add a new method to our Core Data manager to handle this. Add the following inside CoreDataManager.swift:

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, we fetch all of the reviews for a restaurant by their ID. Then, we use the reduce method to add them all together, and finally, we calculate the average. Now, let's use this newly created method. Open up RestaurantDetailViewController.swift. Under the selectedRestaurant variable, add the following:

let manager = CoreDataManager()

Next, under the createRating() method, we just set our rating...