Chapter    36

Class Type Properties

Type properties belong to a particular type, in contrast to the properties discussed in Chapter 34, which belong to instances of a type. You use type properties when you want to have properties that are universal to all instances of that type.

You use the class keyword to declare a type property for a class (see Listing 36-1).

Listing 36-1. Declaring Class Type Properties

class Person {    class var species:String{        return "Homo sapiens"    }    var name: String = "Name"    var age:Int = 0    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }}

In Listing 36-1, you used the class keyword to define the type property species. Type properties for classes must ...

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.