Swift 5-Star Rating System of Core Data Records

Mazen Kilani
4 min readJun 6, 2021

Users Rate App Records by a 5-Star Rating System

AppsGym Books Rating Stars

Scenario

Our app allows users to rate the records (books) using a similar setup to Amazon’s 5-star rating system. Each of the stars can either be empty (grey star) or filled (yellow star). The user can

Do nothing, leaving the 5 stars empty

Assign Rating Stars, from 1 to 5 (the number of filled yellow stars)

Clear the Rating Stars, if the user changes their mind after assigning stars

This article will replicate Amazon’s rating style (0, 1 to 5 stars), as our practical example is related to books. However, the app entities can be cafés, recipes, or any other entity.

GIF Book Rating Stars in New Book and Book Detail Views

Technique

The star visual representation and interactivity (user can tap any star) will need a button @IBOutlet for each star (1 to 5), as well as a button to clear all stars (0 stars). The actual ‘rating’ value will be a text, e.g., “0Stars”, “1Star”, “5Stars” (naturally, you can choose to use a numeric value: 0 to 5). Each rating star will have 2 associated images: Star_Empty (blank star) and Star_Solid (yellow star).

Each of the 6 star-buttons will have a unique Tag (0 to 5). All the star buttons will be linked to one@IBAction, with a Switch statement on the button tag. The tag corresponding case turns each star ON (image Star_Solid yellow) or OFF (image Star_Empty).

Note: If the user taps any star, then the tapped star and all the stars before it will turn ON (image Star_Solid ) and all the stars after it will turn OFF (image Star_Empty). If the user taps the Clear Stars, the app turns all stars OFF.

User Taps the 3rd Star: 1–3 On, 4 and 5 Off

Audience

The article is for Swift developers who seek complete, proven, code-centric solutions to speed up their development projects. The code snippets below can be used with minimal customisation.

App Model

We based the article on the AppsGym Books model app, published on Apple’s App Store (as 8Books), and you can download the complete Xcode project on AppsGym.com, for free.

User Interface

The NewBookTableViewController and the BookDetailsViewController share the same, but not identical, visual representation of the Rating Stars:

New Book

New Book Storyboard

Book Details

Book Details Storyboard

Logic

NewBookTableViewController.swift & BookDetailsViewController.swift will have 6 @IBOutlets, 1 var rating (String) and 1 @IBAction func ratingSelected(..), with a switch statement based on the sender.tag (the UIButton and its tag value).

The NewBookTableViewController will display all stars empty. The BookDetailsViewController will display the stars as per current rating in the Core Data record. In both cases, the logic is the same afterwards: the user can tap/touch any star to indicate their chosen rating or clear all stars.

Note: You may like to evaluate alternative ways to display and execute the 5- stars system (e.g., touchesBegan location, Gesture Recogniser location), and you can also employ different code techniques (e.g., array of buttons, for 0…5 loops). Our choice for this article, a bit manual and repetitive in setup, is chosen for its simplicity and ease of implementation.

Code

Code: NewBookTableViewController.swift

NewBookTableViewController.swift ratingSelected(..)

Code: BookDetailsViewController.swift

BookDetailsViewController.swift Initialise and viewDidLoad()

BookDetailsViewController.swift ratingSelected(..)

BookDetailsViewController.swift setRatingStars(..)

The article covered the implementation of a user rating stars system in our model Books app. Any book can have a 1 to 5 stars rating, or none. The user can set up the rating upon adding a new book, or later in the book details view. Naturally, the rating stars system can be applied to any app entities (cafés, recipes, photos, etc.). Hope you find it useful in your app. Thanks for reading!

--

--

Mazen Kilani

I published 47 Swift iOS apps/games and 2 Flutter Android apps. I share complete Xcode projects, free (no ads), at AppsGym.com.