O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required