Book Image

iOS 13 Programming for Beginners - Fourth Edition

By : Ahmad Sahar, Craig Clayton
Book Image

iOS 13 Programming for Beginners - Fourth Edition

By: Ahmad Sahar, Craig Clayton

Overview of this book

iOS 13 comes with features ranging from Dark Mode and Catalyst through to SwiftUI and Sign In with Apple. If you're a beginner and are looking to experiment and work with these features to create your own apps, then this updated fourth edition gets you off to a strong start. The book offers a comprehensive introduction for programmers who are new to iOS, covering the entire process of learning the Swift language, writing your own apps, and publishing them on the App Store. This edition is updated and revised to cover the new iOS 13 features along with Xcode 11 and Swift 5. The book starts with an introduction to the Swift programming language, and how to accomplish common programming tasks with it. You'll then start building the user interface (UI) of a complete real-world app, using the latest version of Xcode, and also implement the code for views, view controllers, data managers, and other aspects of mobile apps. The book will then help you apply the latest iOS 13 features to existing apps, along with introducing you to SwiftUI, a new way to design UIs. Finally, the book will take you through setting up testers for your app, and what you need to do to publish your app on the App Store. By the end of this book, you'll be well versed with how to write and publish apps, and will be able to apply the skills you've gained to enhance your apps.
Table of Contents (33 chapters)
1
Section 1: Swift
10
Section 2: Design
15
Section 3: Code
26
Section 4: Features

Calculating a restaurant's overall rating

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:

  1. 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...