Chapter    37

Type Methods

In Chapter 31, you coded a function for the Person class named profile(). Class functions are also known as methods, and in Chapter 31 you coded an instance method. Instance methods require a type instance.

In contrast, type methods are functions that require the type (class, enumeration, or structure) to work. To declare a type method for a class, you must use the class keyword (see Listing 37-1).

Listing 37-1. Declaring Type Methods

class Person {    class var species:String{        return "Homo sapiens"    }    class func printDescription() {        println("The Person class defines the structure to represent an                individual of the species \(species).")    }    var name: String = "Name"    var age:Int ...

Get Swift Quick Syntax Reference 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.