Creating the Line Struct

You are going to create the custom Line type. So far, all of the types that you have created have been classes. In fact, they have been Cocoa Touch subclasses; for example, you have created subclasses of NSObject, UIViewController, and UIView.

Line will be a struct. You have used structs throughout this book – CGRect, CGSize, and CGPoint are all structs. So too are String, Int, Array, and Dictionary. Now you are going to create one of your own.

Create a new Swift file named Line.

In Line.swift, import CoreGraphics and declare the Line struct. Declare two CGPoint properties that will determine the beginning and ending point for the line.

import Foundation
import CoreGraphics

struct Line {
    var begin = CGPoint.zero ...

Get iOS Programming: The Big Nerd Ranch Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.