Book Image

Swift Cookbook

Book Image

Swift Cookbook

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
About the Author
About the Reviewers

Dangerous moves

Quick look is a good tool that helps us to visualize the current status of an object, but sometimes when we create our own class with its own logic, quick look won't be able to draw something that represents the object without your help.

In this recipe, we will learn how to create our own class representation. To do this, we will create a class that represents a checker board.

Getting ready

Create a new playground called Chapter 6 Checkers with the iOS option selected. If you select MacOS, remember that you should import Cocoa and replace some types with the equivalent one, such as NSBezierPath instead of UIBezierPath.

How to do it...

Let's create our checkerboard that makes dangerous moves:

  1. Let's start creating a class that inherits from NSObject:

    class CheckersBoard:NSObject {

    Before we go any further, we need to define the status of each square, as you know it could have a black piece, a white piece, or it could be empty:

        enum BoardSpace {
            case FREE,