Ball

public class Ball : Hashable, CustomStringConvertible

This is a simple ball object that has a volume property.

It also illustrates how to make a class Hashable (so it can be stored in sets, as keys of Maps, etc.), and how to enable it to be printed in a meaninful way.

Note the computed properties for the implemented Protocols. This will be a common pattern for your classes.

  • Computed property for CustomStringConvertible protocol

    Declaration

    Swift

    public var description: String { get }
  • Computed property for Hashable protocol

    Declaration

    Swift

    public var hashValue: Int { get }
  • Determine whether two balls are equal. Balls utilize pointer equality.

    Declaration

    Swift

    public static func == (lhs: Ball, rhs: Ball) -> Bool

    Return Value

    true if and only if lhs and rhs are the same Ball object.

  • The volume of the Ball

    Declaration

    Swift

    public var volume: Double { get }